0% found this document useful (0 votes)
17 views34 pages

Android App Development Basics

The document provides a series of Android application examples, including creating a simple 'Hello World' app, setting a background image, using buttons and text views, starting new activities with intents, passing data between activities, displaying toast notifications, implementing list views, and using radio buttons. Each example includes the necessary XML layout files and Java code for the MainActivity and any additional activities. The document serves as a tutorial for basic Android app development concepts and practices.

Uploaded by

jonwik6965
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views34 pages

Android App Development Basics

The document provides a series of Android application examples, including creating a simple 'Hello World' app, setting a background image, using buttons and text views, starting new activities with intents, passing data between activities, displaying toast notifications, implementing list views, and using radio buttons. Each example includes the necessary XML layout files and Java code for the MainActivity and any additional activities. The document serves as a tutorial for basic Android app development concepts and practices.

Uploaded by

jonwik6965
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) Create an Application to print “Hello World”.

Source code:-
activity_main.xml

<LinearLayout

xmlns:android="[Link]
id"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="@string/hello_world" />

</LinearLayout>

[Link]

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
Output
2) Create an Application to set background image.

Source code:-

activity_main.xml

<LinearLayout
xmlns:android="[Link]
id"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/image"
>

</LinearLayout>

[Link]

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
}

}
Output:
3) Create Application with basic views like Button and Text View.

Source code:-
activity_main.xml

<LinearLayout
xmlns:android="[Link]
id"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="Text View" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="Button" />

</LinearLayout>

[Link]

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
}

}
Output
4) Create an application which will start another activity using Intent.

Source code:-
activity_main.xml

<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="Click To Go Next Activity" />

</LinearLayout>

next_activity.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="Welcome to Next Activity" />

</LinearLayout>

[Link]

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="[Link]
package="[Link].intent_6"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="[Link].intent_6.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />

<category
android:name="[Link]" />
</intent-filter>
</activity>
<activity
android:name="[Link].intent_6.NextActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />
<category
android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>

[Link]

public class MainActivity extends Activity implements


OnClickListener{

Button btn1 ;

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);

btn1 = (Button) findViewById([Link].btn1);


[Link]((OnClickListener)
this);
}

@Override
public void onClick(View a) {
// TODO Auto-generated method stub
Intent inext = new
Intent([Link],[Link]);
startActivity(inext);
}

}
[Link]

public class NextActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].next_activity);
}
}

Output:
5) Create an application which will pass data to second activity.

Source code:-

activity_main.xml

<LinearLayout
xmlns:android="[Link]
id"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="left"
android:hint="Text Here"
/>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="Submit" />

</LinearLayout>

next_activity.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:layout_gravity="center_horizontal"
android:text="" />

</LinearLayout>
[Link]

public class MainActivity extends Activity implements


OnClickListener{

Button btn1 ;
EditText et1 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

btn1 = (Button) findViewById([Link].btn1);


et1 = (EditText) findViewById([Link].et1);
[Link]((OnClickListener) this);
}

@Override
public void onClick(View a) {
// TODO Auto-generated method stub
String data = [Link]().toString();

Intent inext = new


Intent([Link],[Link]);
[Link]("data1",data);
startActivity(inext);
}

}
[Link]

public class NextActivity extends Activity{

TextView tv1 ;

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].next_activity);

tv1 = (TextView) findViewById([Link].tv1);

Intent iget = getIntent();


String s1 =
[Link]().getString("data1").toString();
[Link]("" + s1);
}

[Link]

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="[Link]
package="[Link].intent_6"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="[Link].intent_6.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />

<category
android:name="[Link]" />
</intent-filter>
</activity>
<activity
android:name="[Link].intent_6.NextActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />

<category
android:name="[Link]" />
</intent-filter>
</activity>
</application>

</manifest>

Output
6) Create an application to show Toast notification.

Source code:-

activity_main.xml
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="TOAST"/>

</LinearLayout>
[Link]
public class MainActivity extends Activity {
Button btn1;
Toast t;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btn1=(Button)findViewById([Link].btn1);

[Link](new [Link]() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t=[Link]([Link],"This is Toast
Msg",Toast.LENGTH_LONG);
[Link]();
}
});
}

Output
7) Create an application to show List view .

Source code:-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="[Link]
id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tvb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Item"
android:textColor="#000000"
/>
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</ListView>

</LinearLayout>
[Link]

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="[Link]
id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tvs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Child item"
android:textColor="#000000"></TextView>

<ListView
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</ListView>

</LinearLayout>
[Link]

public class MainActivity extends Activity implements


OnItemClickListener{
ListView lv1 ;
String br[]={"item 1","item 2"};

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
[Link](savedInstanceState);
setContentView([Link].activity_main);
lv1 = (ListView) findViewById([Link].lv1);
ArrayAdapter<String>aaa=new
ArrayAdapter<String>(this,[Link].simple_list_
item_1,br);

[Link](aaa);

[Link]((OnItemClickListener)
this);
}

public void onItemClick(AdapterView<?> arg0, View


arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;

Intent iw = new Intent([Link],


[Link]);
[Link]("pos", arg2);
startActivity(iw);
}
}
[Link]

public class SecondActivity extends Activity implements


OnItemClickListener {
ListView lvs;
String
sm[]={"First","Second","Third","Fourth","Fifth","Sixth"
,"Seventh","Eighth"};

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
[Link](savedInstanceState);
setContentView([Link]);
lvs = (ListView) findViewById([Link].lv2);
ArrayAdapter<String>aa=new
ArrayAdapter<String>(this,[Link].simple_list_
item_1,sm);

[Link](aa);
[Link](this);

@Override
public void onItemClick(AdapterView<?> arg0, View
arg1, int arg2, long arg3) {
// TODO Auto-generated method stub

TextView tv = (TextView)arg1;
Intent i = getIntent();
Integer s1 =[Link]().getInt("pos");
Intent i1 = new
Intent("[Link]");
[Link]("pos", s1);
[Link]("pos1", arg2);
startActivity(i1);
}
}
[Link]

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="[Link]
package="[Link].listview_7"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="[Link].listview_7.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />

<category
android:name="[Link]" />
</intent-filter>
</activity>
<activity
android:name="[Link].listview_7.SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="[Link]" />

<category
android:name="[Link]" />
</intent-filter>
</activity>
</application>

</manifest>
Output
8) Create an application to show Radio buttons.

Source code:-

activity_main.xml
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<RadioGroup
android:id="@+id/rgclr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">

<RadioButton
android:id="@+id/rbred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"/>

<RadioButton
android:id="@+id/rbblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"/>

<RadioButton
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"/>

</RadioGroup>

</LinearLayout>

[Link]

public class MainActivity extends Activity implements


OnCheckedChangeListener{

private RadioGroup rgclr ;

@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);

rgclr = (RadioGroup) findViewById([Link]);


[Link](this);
}

@Override
public void onCheckedChanged(RadioGroup group, int
checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case [Link]:
break;
case [Link]:
break;
case [Link]:
break;
}

Output
9) Create an application to show check boxes.

Source code:-

[Link]
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cb1"
android:text="THE CHECKBOX IS-UNCHECKED"
/>

</LinearLayout>
[Link]
public class Main extends Activity {
CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);

cb=(CheckBox)findViewById([Link].cb1);
[Link](new checker());

}
class checker implements
[Link]
{
@Override
public void onCheckedChanged(CompoundButton
buttonView,
boolean isChecked) {
if(isChecked){
[Link]("THE CHECKBOX IS-CHECKED");
}
else
{
[Link]("THE CHECKBOX IS-UNCHECKED")}}}}

Output
10) Create an application to show Frame Layout.
Source code:-

activity_main.xml

<FrameLayout
xmlns:android="[Link]
id"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
android:id="@+id/iv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/bush"
android:scaleType="fitXY"
/>

<ImageView
android:id="@+id/iv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/spring"
android:scaleType="fitXY"
/>

</FrameLayout>

[Link]
public class MainActivity extends Activity {

ImageView one = null ;

ImageView two = null ;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);

setContentView([Link].activity_main);

one = (ImageView) findViewById([Link].iv1);


two = (ImageView) findViewById([Link].iv2);

[Link](new OnClickListener() {

@Override
public void onClick(View a) {
// TODO Auto-generated method stub
[Link]([Link]);
[Link]([Link]);
}

});

[Link](new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

[Link]([Link]);
[Link]([Link]);
}

});

Output

You might also like