Android: Open menu programmatically

夙愿已清 提交于 2019-12-24 08:24:14

问题


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:

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


回答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:


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);



回答3:


Use this in your menu layout:

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



回答4:


Hello Dear if you are using toolbar following code

toolbar.showOverflowMenu();

And other wise you can directly call

MainActivity.this.openOptionsMenu();



回答5:


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



来源:https://stackoverflow.com/questions/40668806/android-open-menu-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!