简体   繁体   中英

Android How to open the options menu item after clicking a button?

I'm using menu items like this.

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

    <item
        android:id="@+id/menu_overflow"
        android:enabled="true"
        android:icon="@drawable/ic_menu_moreoverflow_mtrl_alpha"
        android:title=""
        app:showAsAction="ifRoom">
        <menu>
            <item
                android:id="@+id/action_city"
                android:icon="@drawable/ic_change_city"
                android:title="Change City"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_language"
                android:icon="@drawable/ic_change_language"
                android:title="Change Language"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_change_theme"
                android:icon="@drawable/ic_change_theme"
                android:title="Change Theme"
                app:showAsAction="never" />
        </menu>
    </item>

</menu>

Now How can I expand Menu Item -> menu_overflow programmatically after clicking a button?

My code is:

            activity.openOptionsMenu();
            ---------
            toolbar.showOverflowMenu();
            ---------
            menu_overflow.expandActionView();
            ---------
            menu_overflow.getSubMenu().getItem().expandActionView();

But those above codes are not working.

Here is design how it looks like. 在此处输入图像描述

          Create one xml file names as menu_main. 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="example.javatpoint.com.optionmenu.MainActivity">  

<item  android:id="@+id/item1"  
    android:title="Item 1"
    android:icon="@drawable/ic_action_accept" />  
<item  android:id="@+id/item2"  
    android:title="Item 2"/>  
<item  android:id="@+id/item3"  
    android:title="Item 3"  
    app:showAsAction="withText"/>  
  </menu>  



       Then add these code in your Activity :


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



@Override  
public boolean onOptionsItemSelected(MenuItem item) {  
   int id = item.getItemId();  
    switch (id){  
        case R.id.item1:  
            Toast.makeText(getApplicationContext(),"Item 
             Selected",Toast.LENGTH_LONG).show();  
            return true;  
        case R.id.item2:  
            Toast.makeText(getApplicationContext(),"Item 2 
     Selected",Toast.LENGTH_LONG).show();  
            return true;  
        case R.id.item3:  
            Toast.makeText(getApplicationContext(),"Item 3 
            Selected",Toast.LENGTH_LONG).show();  
            return true;  
        default:  
            return super.onOptionsItemSelected(item);  
             }  
                 }  

Try this

YourActivity.this.openOptionsMenu();

Use Activity.openOptionsMenu(), 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 automatically show sub menu. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM