1) Create an Application to print “Hello World”.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
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>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Output
2) Create an Application to set background image.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/image"
>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output:
3) Create Application with basic views like Button and Text View.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
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>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output
4) Create an application which will start another activity using Intent.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="http://schemas.android.com/apk/res/android"
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>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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="com.example.intent_6.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.intent_6.NextActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity implements
OnClickListener{
Button btn1 ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener((OnClickListener)
this);
}
@Override
public void onClick(View a) {
// TODO Auto-generated method stub
Intent inext = new
Intent(MainActivity.this,NextActivity.class);
startActivity(inext);
}
}
NextActivity.java
public class NextActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activity);
}
}
Output:
5) Create an application which will pass data to second activity.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
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="http://schemas.android.com/apk/res/android"
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>
MainActivity.java
public class MainActivity extends Activity implements
OnClickListener{
Button btn1 ;
EditText et1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
et1 = (EditText) findViewById(R.id.et1);
btn1.setOnClickListener((OnClickListener) this);
}
@Override
public void onClick(View a) {
// TODO Auto-generated method stub
String data = et1.getText().toString();
Intent inext = new
Intent(MainActivity.this,NextActivity.class);
inext.putExtra("data1",data);
startActivity(inext);
}
}
NextActivity.java
public class NextActivity extends Activity{
TextView tv1 ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activity);
tv1 = (TextView) findViewById(R.id.tv1);
Intent iget = getIntent();
String s1 =
iget.getExtras().getString("data1").toString();
tv1.setText("" + s1);
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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="com.example.intent_6.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.intent_6.NextActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Output
6) Create an application to show Toast notification.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
MainActivity.java
public class MainActivity extends Activity {
Button btn1;
Toast t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t=Toast.makeText(MainActivity.this,"This is Toast
Msg",Toast.LENGTH_LONG);
t.show();
}
});
}
Output
7) Create an application to show List view .
Source code:-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
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>
Second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
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>
MainActivity.java
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
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (ListView) findViewById(R.id.lv1);
ArrayAdapter<String>aaa=new
ArrayAdapter<String>(this,android.R.layout.simple_list_
item_1,br);
lv1.setAdapter(aaa);
lv1.setOnItemClickListener((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(MainActivity.this,
SecondActivity.class);
iw.putExtra("pos", arg2);
startActivity(iw);
}
}
SecondActivity.java
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
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
lvs = (ListView) findViewById(R.id.lv2);
ArrayAdapter<String>aa=new
ArrayAdapter<String>(this,android.R.layout.simple_list_
item_1,sm);
lvs.setAdapter(aa);
lvs.setOnItemClickListener(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 =i.getExtras().getInt("pos");
Intent i1 = new
Intent("android.intent.action.LAST");
i1.putExtra("pos", s1);
i1.putExtra("pos1", arg2);
startActivity(i1);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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="com.example.listview_7.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.listview_7.SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Output
8) Create an application to show Radio buttons.
Source code:-
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
MainActivity.java
public class MainActivity extends Activity implements
OnCheckedChangeListener{
private RadioGroup rgclr ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgclr = (RadioGroup) findViewById(R.id.rgclr);
rgclr.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int
checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbred:
break;
case R.id.green:
break;
case R.id.rbblue:
break;
}
Output
9) Create an application to show check boxes.
Source code:-
main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
Main.java
public class Main extends Activity {
CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb=(CheckBox)findViewById(R.id.cb1);
cb.setOnCheckedChangeListener(new checker());
}
class checker implements
android.widget.CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton
buttonView,
boolean isChecked) {
if(isChecked){
cb.setText("THE CHECKBOX IS-CHECKED");
}
else
{
cb.setText("THE CHECKBOX IS-UNCHECKED")}}}}
Output
10) Create an application to show Frame Layout.
Source code:-
activity_main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
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>
MainActivity.java
public class MainActivity extends Activity {
ImageView one = null ;
ImageView two = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (ImageView) findViewById(R.id.iv1);
two = (ImageView) findViewById(R.id.iv2);
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View a) {
// TODO Auto-generated method stub
two.setVisibility(View.VISIBLE);
a.setVisibility(View.GONE);
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
one.setVisibility(View.VISIBLE);
v.setVisibility(View.GONE);
}
});
Output