How to add overflow menu to Toolbar?

前端 未结 3 817
粉色の甜心
粉色の甜心 2020-12-09 16:09

I\'m trying to use Android ActionBar in my app, and have an option that\'s hidden away in the overflow menu.

There\'s a lot of documentation out there,

相关标签:
3条回答
  • 2020-12-09 16:53

    in manifest file declare

    android:theme="@style/AppTheme.NoActionBar"
    

    like this :

    <activity
        android:name=".ActivityName"
        android:label="@string/label"
        android:theme="@style/AppTheme.NoActionBar" />
    

    and add this to your style :

     <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    and call this in Activity onCreate() :

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    override this method in activity:

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.product_list, menu);
        //U can find item set icon and stuff...
        MenuItem item= menu.findItem(R.id.action_search);
    
        return true;
    }
    

    and declare your menu like this for overflow menu:

      <?xml version="1.0" encoding="utf-8"?>
    <menu >
        <group>
            <item
                android:id="@+id/sign_out"
                android:title="@string/sign_out" />
            <item
                android:id="@+id/about"
                android:title="@string/about" />
        </group>
    </menu>
    

    and for handle item selection call this

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) { 
           case R.id.sign_out:
           //do stuff
               break;
               }  
            return super.onOptionsItemSelected(item);
        }
    

    done :)

    0 讨论(0)
  • 2020-12-09 16:57

    Define a Menu for your Toolbar in the res/menu resource folder, for example:

    toolbar_menu.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"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".activity.MainActivity">
    
        <item
            android:id="@+id/action_sign_out"
            android:title="@string/toolbar_sign_out"
            app:showAsAction="never"/>
    </menu>
    

    Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.

    The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).

    In your Activity, set up your Toolbar:

    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
    

    And override onCreateOptionsMenu() to inflate your previously defined menu resource:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }
    

    You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_sign_out: {
                // do your sign-out stuff
                break;
            }
            // case blocks for other MenuItems (if any)
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-12-09 16:59

    Simple do This copy this code on your MainActivet`

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main2, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                Group gp=(Group)findViewById(R.id.order);
    
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }`
    

    Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name

    and past this code on menu2.xml file

        <?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_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never"
            android:icon="@android:drawable/ic_input_add"
            />
    
    </menu>
    

    if any Query Please text me

    0 讨论(0)
提交回复
热议问题