Options Menu with Suboptions
Example Code:
MainActivity.java:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 package com.example.subitems; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_example, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.item1: Toast.makeText(this, "Option 1 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.item2: Toast.makeText(this, "Option 2 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem1: Toast.makeText(this, "SubOption 1 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem2: Toast.makeText(this, "SubOption 2 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem3: Toast.makeText(this, "SubOption 3 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem4: Toast.makeText(this, "SubOption 4 is selected", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package com.example.subitems; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_example, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.item1: Toast.makeText(this, "Option 1 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.item2: Toast.makeText(this, "Option 2 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem1: Toast.makeText(this, "SubOption 1 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem2: Toast.makeText(this, "SubOption 2 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem3: Toast.makeText(this, "SubOption 3 is selected", Toast.LENGTH_SHORT).show(); return true; case R.id.subitem4: Toast.makeText(this, "SubOption 4 is selected", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } } |
menu_example.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/item1" android:title="Option 1" app:showAsAction="never"> <menu> <item android:id="@+id/subitem1" android:title="Option 1.1"/> <item android:id="@+id/subitem2" android:title="Option 1.2"/> </menu> </item> <item android:id="@+id/item2" android:title="Option 2" app:showAsAction="never" > <menu> <item android:id="@+id/subitem3" android:title="Option 2.1"/> <item android:id="@+id/subitem4" android:title="Option 2.2"/> </menu> </item> </menu> |