Create menu.xml file New->Android Xml file
Filename -> menu.xml and select xml file type as menu.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/Menu1" android:title="Menu 1" android:orderInCategory="1" /> <item android:id="@+id/Menu2" android:orderInCategory="2" android:title="Menu 2" /> <item android:id="@+id/Menu3" android:orderInCategory="3" android:title="Menu 3" /> <item android:id="@+id/submenu" android:title="Sub menu" android:orderInCategory="4"> <menu> <item android:id="@+id/submenu1" android:title="Sub menu 1" /> <item android:id="@+id/submenu2" android:title="Sub menu 2" /> </menu> </item> </menu>
MenuActivity.java public class MenuActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(getApplication()) .inflate(R.menu.menu, menu); return(super.onPrepareOptionsMenu(menu)); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.Menu1: Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show(); break; case R.id.Menu2: Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show(); break; case R.id.Menu3: Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show(); break; case R.id.submenu: Toast.makeText(this, "Sub menu", Toast.LENGTH_SHORT).show(); break; } return(super.onOptionsItemSelected(item)); } }
SnapShot:
Filename -> menu.xml and select xml file type as menu.
0 comments: