Download Code

List Menu

In this tutorial you will learn how to create dynamically list menu in android application and also how to start an activity using Intent when any list item is clicked.So first i’ll give you some explanation about List Activity.

List Activity

Activity that displays a list of items by attaching their particular functionality when clicked on particular item.List Activity holds the object of List View to bound different data sources like array.Detail

Let’s start.

Create new android project New>Android Application Project

android list menu,android list menu example,list menu

click Next until Finish will enable then click finish.Now you are not concerning about XML Layout.You will deal with your activity because you will done everything for dynamic list menu.Close XML Layout if display at your screen simply.

Open your MainActivty.java class which is by default created when you created your android application project.

It will look like by this

android list menu,android list menu example,list menu

by default your MainActivity might extends with “Activity” or “ActionBarActivity” so you don’t need both of them.Here you have to extends your MainActivity with ListActivity and remove all methods like onCreate and other two.Then your MainActivity will look like this.

android list menu,android list menu example,list menu

Next right click on List Activity or anywhere within MainActivity.java class select Source > Override/Implement Methods.New window will open now within ListActivity category and check onListItemClick method next scroll down and go into Activity open this up and find onCreate(Bundle) method and check it now hit OK.After that your Activity will look like this.

android list menu,android list menu example,list menu

Next what you will do is to setup a string array for your list menu to display in a view.You can create string array just like this.

public class MainActivity extends ListActivity {

	
	// string array for list menu
	
	String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
}

Now before going to next step i’ll give you some introduction about setListAdapter method and ArrayAdapter.

Set List Adapter

This is basically a public method of List Activity class which provide cursor to list items.

Array Adapter

Array Adapter contained array or backed by array.It’s a BaseAdapter.

So now you will set your string array to list adapter using array adapter within onCreate method because by default this method is called so you have to do it within this method.Then your code will look like this.

public class MainActivity extends ListActivity {
	// string array for list menu
	String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setListAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,androidCodec));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
}

If you run your android application project then output will be look like this.

android list menu,android list menu example,list menu
Now next you will start new activity via intent using Class variable.So what is Class?

Class

Class is in-memory represetation which serves as a starting point.

Now you are in onListItemClick method.First you have to get text of which item clicked by the user so that particular activity get started.You can do this by passing position variable to anroidCodec array which is an index(position).Just like this.

public class MainActivity extends ListActivity {

	
	// string array for list menu
	
	String androidCodec[] = {"Java","C#","C++","Php","Javascript","Android","Codec"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setListAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,androidCodec));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		//passing position index in androidCodec array to get that particular text
		String getTxt = androidCodec[position];
	}
}

Next you will setup a Class variable and get name of an activity by using forName method and pass that variable to an intent to start an activity which you want on one item clicked.You must surround Class variable by try Catch to handle exceptions.One thing don’t forget your list item name and activity name should be same.Final Code will be like.

public class MainActivity extends ListActivity {

	
	// string array for list menu
	
	String androidCodec[] = {"AndroidCodec","Java","C#","C++","Php","Javascript","Android","Codec"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setListAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,androidCodec));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		//passing position index in androidCodec array to get that particular text
		String getTxt = androidCodec[position];
		Class getName;
		try {
			getName = Class.forName("androidcodec.listactivity.AndroidCodec");
			Intent n = new Intent(MainActivity.this,getName);
			startActivity(n);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

Now Run your android application project list Menu will look like this.

android list menu,android list menu example,list menu

Then when you click on “AndroidCodec” item new activity will start just like that i haven’t put anything in that activity.

android list menu,android list menu example,list menu

I hope you got everything in this tutorial.

Thanks.