Hi, Today i’ll explain how to create a Menu Page by using the default resources available with android. This is the easiest way to create a menu. You can use the ListView widget to create the menu list.
Layout page Design
- Goto res-> layout-> Right click on the layout folder and add a new Android XML file.
- Now select the “Graphical Layout” view and drag and drop the ListView widget available under “Composite”.
The menu.xml file is generated as follows.
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<ListView
android:id=”@+id/listView1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” >
</ListView>
</LinearLayout>
Things to consider when implementing
- Create a String array including the list of items of the menu.
- Use the simple_list_item_1 when creating the ArrayAdapter.
- ** Change the id of the ListView at the XML file as android:id=”@android:id/list”. Otherwise you’ll get a RuntimeException.
Implementation of ListActivity
public class HelloWorldActivity extends ListActivity {
/** Called when the activity is first created. */
static final String[] s = new String[] { “Service1″,”Service2″,”Service3″,”Service4″,”Service5” };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, s));
getListView().setTextFilterEnabled(true);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String s2 = o.toString();
Toast.makeText(this, “You have chosen the service:” + “” + s2,
Toast.LENGTH_LONG).show();
if (position == 0) {
Intent intent = new Intent(HelloWorldActivity.this,
AnotherService.class);
startActivity(intent);
}
}
}