3

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?

1

6 Answers 6

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

In toolbar case don't forget to inflate menu first. Then call showOverflowMenu();
2

Hello Dear if you are using toolbar following code

toolbar.showOverflowMenu();

And other wise you can directly call

MainActivity.this.openOptionsMenu();

1 Comment

In toolbar case don't forget to inflate menu first. Then call showOverflowMenu();
1

Use:

MainActivity.this.openOptionsMenu();

Then, check your toolbar if is this okay.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acercade_activity);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
}

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            //case R.id.action_settings:
            //    return true;

            case R.id.perfil:
                drawerLayout.openDrawer(Gravity.LEFT);
                return true;
            default:
                return super.onOptionsItemSelected(item);



        }

    }

2 Comments

Yes this works if the MainActivity is extended by 'Activity' class, but strangely if it's extended by 'AppCompatActivity', it doesn't work. Any idea about that?
Check de new update, your problem is the toolbar. @nick
1

Use this in your menu layout:

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

Comments

0

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

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.