SubMenu

Menu, SubMenu

/res/layout/main.xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

/res/menu/menus.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/Menu1" android:orderInCategory="1"
 android:title="Add" />
 <item android:id="@+id/submenu" android:title="Send"
 android:orderInCategory="2">
 <menu>
 <item android:id="@+id/submenu1" android:title="Via BlueTooth" />
 <item android:id="@+id/submenu2" android:title="Via SMS" />
 </menu>
 </item>
 <item android:id="@+id/Menu3" android:orderInCategory="3"
 android:title="Copy" />
 <item android:id="@+id/Menu4" android:title="Delete"
 android:orderInCategory="4" />
</menu>

Activity file

package com.androidwork.submenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class SubmenudemoActivity extends Activity {
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		new MenuInflater(getApplication()).inflate(R.menu.menus, menu);
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.Menu1:
			Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
			break;
		case R.id.submenu:
			Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
			break;
		case R.id.Menu3:
			Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();
			break;
		case R.id.Menu4:
			Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
}

Output

Advertisements

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