Android Illustration To Practice Grouped Sub-Optionsmenu

Posted on

Create menu/activity_main.xml

<?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/option1"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Android representative to create grouped sub-OptionsMenu"/>     <item         android:id="@+id/option2"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Android representative to create grouped sub-OptionsMenu"/>     <item         android:id="@+id/option3"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Android representative to create grouped sub-OptionsMenu">         <menu >             <group android:id="@+id/group3"                 android:checkableBehavior="single">                 <item android:id="@+id/submenu3a"                     android:title="Android representative to create grouped sub-OptionsMenu" />                 <item android:id="@+id/submenu3b"                     android:title="Android representative to create grouped sub-OptionsMenu" />             </group>         </menu>      </item> </menu> 

MainActivity.java

package com.blogspot.android_er.androidoptionsmenu;  import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast;  populace degree MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      @Override     populace boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         provide super.onCreateOptionsMenu(menu);     }      @Override     populace boolean onOptionsItemSelected(MenuItem item) {         switch(item.getItemId()){             instance R.id.option1:                 Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_LONG).show();                 provide true;             instance R.id.option2:                 Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_LONG).show();                 provide true;             instance R.id.submenu3a:                 Toast.makeText(MainActivity.this, "Option three A", Toast.LENGTH_LONG).show();                 provide true;             instance R.id.submenu3b:                 Toast.makeText(MainActivity.this, "Option three B", Toast.LENGTH_LONG).show();                 provide true;         }         provide super.onOptionsItemSelected(item);     } }