'Android: Open menu programmatically

I've been trying to add a functionality to my android application such that when I click a button, menu listing should be visible:

Here is my code:

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.MainActivity" >

    <item android:id="@+id/action_onthego_sentence"
          android:title="settings"
          android:orderInCategory="100"
          app:showAsAction="never" />

</menu>

From the main activity, upon clicking a button, I do:

        button.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View view )
            {
                runOnUiThread( new Runnable()
                {
                    @Override
                    public void run()
                    {
                        openOptionsMenu();
                    }
                } );
            }
        } );

What I need is:

enter image description here

As shown in the image, I'd like to see the menu is opened. Any suggestions please?



Solution 1:[1]

If you are using customized toolbar in your app, then the following way will be useful,

 new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    toolbar.showOverflowMenu();
                }
            }, 500);

Solution 2:[2]

Use this in your menu layout:

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Setting"
app:showAsAction="ifRoom" />

Solution 3:[3]

Hello Dear if you are using toolbar following code

toolbar.showOverflowMenu();

And other wise you can directly call

MainActivity.this.openOptionsMenu();

Solution 4:[4]

Try the below solution It will be display click on the button menu listing should be visible:

res/menu/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/action_changeLang"
        android:orderInCategory="100"
        android:title="Change Lang" />
    <item
        android:id="@+id/action_color"
        android:orderInCategory="100"
        android:title="Select color" />
    <item
        android:id="@+id/action_applist"
        android:orderInCategory="100"
        android:title="App List" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_changeLang:
            // Add your code
            break;
        case R.id.action_color:
            // Add your code
            break;
        case R.id.action_applist:
            // Add your code
            break;
    }
    return super.onOptionsItemSelected(item);
}

Try to use above solution. It will work for me

Solution 5:[5]

Use Activity.openOptionsMenu() to open menu programmatically, but you may need to post delay to call it. like below

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        openOptionsMenu();
    }
}, 1000);  

If you need to open sub menu automatically. you can call menu.performIdentifierAction after openOptionsMenu, like below

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        openOptionsMenu();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mainMenu.performIdentifierAction(R.id.submenu, 0);
            }
        }, 500);
    }
}, 1000);  

Android Dev efficiency tools: https://play.google.com/store/apps/details?id=cn.trinea.android.developertools

Android Open Source Projects: https://p.codekk.com/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nanda Gopal
Solution 2 Shashank Verma
Solution 3 Dhaval Solanki
Solution 4 gotwo
Solution 5 Trinea