Create Menu in Android

Hy Guys…
This time i want to write “How to create menu in android”. Ok lets to try.
1. Create menu in directory /menu/xml_file.xml. example : menu/menu.xml
code like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item01" android:title="item1" android:icon="@drawable/image1"/>
<item android:id="@+id/item02" android:title="item2" android:icon="@drawable/image2"/>
</menu>

2. add this code in your mainActivity.
code like this:


@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item01:
			//add function here
			return true;

		case R.id.item02:
			//add function here

			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

happy codding🙂 wahihihi😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s