Search code examples
androidsubmenuoptionmenu

android how to get menu like below image


In my activity there is an option menu, for an item there is a submenu. I want for a submenu item below will comemenu style

i have xml like this,

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/file"
          android:icon="@drawable/file"
          android:title="@string/file" >
        <!-- "file" submenu -->
        <menu>
            <item android:id="@+id/create_new"
                  android:title="@string/create_new" />
            <item android:id="@+id/open"
                  android:title="@string/open" />
        </menu>
    </item>
</menu>

I know for option menu it is possible but i want to put for a submenu item. How can i do that? Is there any other way to do this?


Solution

  • You should display your own dialog isntead of using menu:

    http://developer.android.com/guide/topics/ui/dialogs.html

    enter image description here

    final CharSequence[] items = {"Red", "Green", "Blue"};
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a color");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alert = builder.create();