0% found this document useful (0 votes)
75 views23 pages

Step 1: Step 2: Step 3: Step 4

The document describes how to develop an Android application that uses various event listeners like button click listener, checkbox change listener and radio button change listener. It provides the steps to create a project, add buttons, checkboxes and radio buttons from the GUI, add code to handle click and change events, and display toasts. The code samples show how to code the Java class and XML layout to set onclick and oncheckedchange listeners for the buttons, checkboxes and radio buttons.

Uploaded by

Sriram Kumar
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)
75 views23 pages

Step 1: Step 2: Step 3: Step 4

The document describes how to develop an Android application that uses various event listeners like button click listener, checkbox change listener and radio button change listener. It provides the steps to create a project, add buttons, checkboxes and radio buttons from the GUI, add code to handle click and change events, and display toasts. The code samples show how to code the Java class and XML layout to set onclick and oncheckedchange listeners for the buttons, checkboxes and radio buttons.

Uploaded by

Sriram Kumar
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
You are on page 1/ 23

212715205115

EX.NO.1 DEVELOP AN APPLICATION THAT USES VARIOUS EVENT LISTENERS

DATE:

1a.BUTTON-OnClickListener

AIM:

To develop an android application that implements buttons.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop buttons from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the button in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each button.
b. Create reference by using id for each object.
c. Register OnClicklistener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ButtonClick extends Activity {


Button b;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click)
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
212715205115

// TODO Auto-generated method stub


Toast.makeText(getBaseContext(),"successful login",Toast.LENGTH_LONG).show();
}
});
b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"successfully registered",5000).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.button_click, menu);
return true;
}
}

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ButtonClick" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="166dp"
android:layout_marginLeft="40dp"
android:text="Signin" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="47dp"
android:layout_toRightOf="@+id/button1"
212715205115

android:text="Signup" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Cakes and Bakes"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

OUTPUT:

RESULT:

Thus button click is implemented successfully in android application.


212715205115

1b.CHECKBOX-OnCheckedChangeListener

AIM:

To develop an android application that implements checkbox.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop checkboxes from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the checkboxes in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each checkbox.
b. Create reference by using id for each object.
c. Register checkedchangelistener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.sample2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class CkeckClick extends Activity {


CheckBox c1,c2,c3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ckeck_click);
c1=(CheckBox)findViewById(R.id.checkBox1);
c2=(CheckBox)findViewById(R.id.checkBox2);
c3=(CheckBox)findViewById(R.id.checkBox3);
c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)
212715205115

{
Toast.makeText(getBaseContext(),"Blackforest is selected",4000).show();
}
else if(arg1==false)
{
Toast.makeText(getBaseContext(),"Blackforest is not selected",4000).show();
}
}
});
c2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)
{
Toast.makeText(getBaseContext(),"Whiteforest is selected",4000).show();
}
else if(arg1==false)
{
Toast.makeText(getBaseContext(),"Whiteforest is unselected",4000).show();
}
}
});
c3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)
{
Toast.makeText(getBaseContext(),"ChocoTruffle is selected",4000).show();
}
else if(arg1==false)
{
Toast.makeText(getBaseContext(),"ChocoTruffle is unselected",4000).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ckeck_click, menu);
return true;
}
}
212715205115

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CkeckClick" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="86dp"
android:layout_marginTop="90dp"
android:text="blackforest" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="28dp"
android:text="whiteforest" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="32dp"
android:text="chocotruffle" />

</RelativeLayout>
212715205115

OUTPUT:

RESULT:

Thus multiple checkboxes are implemented successfully in android application.


212715205115

1c.RADIOBUTTON-OnCheckedChangeListener

AIM:

To develop an android application that implements radiobutton.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop radiobutton from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the radiobuttons in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each radiobutton.
b. Create reference by using id for each object.
c. Register Oncheckedchangelistener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.sample3;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.Toast;

public class RadioButtonClick extends Activity {


RadioButton r1,r2,r3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_click);
r1=(RadioButton)findViewById(R.id.radioButton1);
r2=(RadioButton)findViewById(R.id.radioButton2);
r3=(RadioButton)findViewById(R.id.radioButton3);
r1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
try{
if(arg1==true)
212715205115

{
Toast.makeText(getBaseContext(),"1kg is selected",Toast.LENGTH_LONG).show();
r1.setChecked(true);
r2.setChecked(false);
r3.setChecked(false);
}
}
catch(Exception e){
}
}
});
r2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
try{
if(arg1==true)
{
Toast.makeText(getBaseContext(),"1/2 kg is selected",Toast.LENGTH_LONG).show();
r1.setChecked(false);
r3.setChecked(false);
}
}
catch(Exception e){
}
}
});
r3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
try{
if(arg1==true)
{
Toast.makeText(getBaseContext(),"pieces is selected",Toast.LENGTH_LONG).show();
r2.setChecked(false);
r1.setChecked(false);
}
}
catch(Exception e){
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.radio_button_click, menu);
return true;
}
}
212715205115

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RadioButtonClick" >

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="1 kg" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:layout_marginTop="40dp"
android:text="1/2 kg" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton2"
android:layout_below="@+id/radioButton2"
android:layout_marginTop="56dp"
android:text="pieces" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:text="select one"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>
212715205115

OUTPUT:

RESULT:

Thus radiobuttons are implemented successfully in android application.


212715205115

1d.RATINGBAR-OnRatingBarChangeListener

AIM:

To develop an android application that implements rating bar.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop ratingbar from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the ratingbar in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each ratingbar.
b. Create reference by using id for each object.
c. Register OnRatingBarChangeListener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.sample4;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class RatingBarClick extends Activity {


RatingBar rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar_click);
rb=(RatingBar)findViewById(R.id.ratingBar1);
rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"your rating is"+arg1,4000).show();
}
});
}
@Override
212715205115

public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rating_bar_click, menu);
return true;
}
}

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RatingBarClick" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_alignParentTop="true"
android:layout_marginLeft="65dp"
android:layout_marginTop="50dp"
android:text="Rate us"
android:textAppearance="?android:attr/textAppearanceLarge" />

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
212715205115

OUTPUT:

RESULT:

Thus rating bar is implemented successfully in android application.


212715205115

1e.SEEKBAR-OnSeekBarChangeListener

AIM:

To develop an android application that implements seekbar.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop seekbar from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the seekbar in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each seekbar.
b. Create reference by using id for each object.
c. Register OnSeekBarChangeListener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {


SeekBar s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s=(SeekBar)findViewById(R.id.seekBar1);
s.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int c;
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your progress is"+c,Toast.LENGTH_LONG).show();
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
212715205115

// TODO Auto-generated method stub


}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
c=arg1;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="95dp" />

</RelativeLayout>
212715205115

OUTPUT:

RESULT:

Thus seekbar is implemented successfully in android application.


212715205115

1f.SPINNER- OnItemSelectedListener

AIM:

To develop an android application that implements spinner.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop spinner from form widgets to the graphical layout of the android
application.
Step 3: Change the name of the spinner in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each spinner.
b. Create reference by using id for each object.
c. Register OnItemSelectedListener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.spqrst;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Spinactivity extends Activity {
Spinner sp;
String[] dept={"it","cse","eee"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinactivity);
sp=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> ap=new
ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item,dept);
sp.setAdapter(ap);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
212715205115

Toast.makeText(getBaseContext(),"your dept is"+dept[arg2],5000).show();

}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.spinactivity, menu);
return true;
}
}

Xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Spinactivity" >

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp" />

</RelativeLayout>
212715205115

OUTPUT:

RESULT:

Thus spinner is implemented successfully in android application.


212715205115

1g.LISTVIEW- OnItemClickListener.

AIM:

To develop an android application that implements listview.

PROCEDURE:

Step 1: Create new Android application project.(file-> new->Android application project->


Application name->next->next->blank Activity->give Activity name->finish)
Step 2: Drag and drop listview from composite to the graphical layout of the android
application.
Step 3: Change the name of the listview in xml file.
Step 4: In the javafile perform the following steps.
a. Create an object for each listview.
b. Create reference by using id for each object.
c. Register OnItemClickListener.
Step 5: Save the file.
Step 6: To run the application, windows->Android virtual device-> create a new emulator.
Step 7: Start the emulator.
Step 8: Right click on the application->run as->Android application.

CODE:

Java file:

package com.example.zxpo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListtActivity extends Activity {
ListView lv;
String[] gadgets={"computer","laptop","phone"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listt);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String>ad=new
ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,gadgets);
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg2==0)
212715205115

{
Intent i= new Intent(ListtActivity.this,FirstitemActivity.class);
startActivity(i);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.listt, menu);
return true;
}
}

Xml file:

First item activity xml:


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstitemActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/img1" />
</RelativeLayout>

Second item activity xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SeconditemActivity" >

<ImageView
android:id="@+id/imageView1"
212715205115

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:src="@drawable/img2" />

</RelativeLayout>

OUTPUT:

RESULT:

Thus listview is implemented successfully in android application.

You might also like