57

How to change the index icon of option menu?

enter image description here

I mean icon (3).

Here is my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options, menu);

    return true;
}

And here is the XML file:

<item android:id="@+id/Bugreport"
    android:title="@string/option_bugreport" />

<item android:id="@+id/Info"
    android:title="@string/option_info" />

<item android:id="@+id/About"
    android:title="@string/option_about" />

Jonik
  • 80,077
  • 70
  • 264
  • 372
smartmouse
  • 13,912
  • 34
  • 100
  • 166

13 Answers13

81

The following lines should be updated in app -> main -> res -> values -> Styles.xml

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_launcher</item>
    <item name="android:background">?android:attr/actionBarItemBackground</item>
    <item name="android:contentDescription">"Lala"</item>
</style>

This is how it can be done. If you want to change the overflow icon in action bar

Nouman Bhatti
  • 1,341
  • 6
  • 28
  • 54
Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
64

I got a simpler solution which worked perfectly for me :

Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
        toolbar.setOverflowIcon(drawable);
Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • for me also it worked fine and programmatic way, we can change it any time that is the added advantage. Thanks Parth. – Swapnil May 10 '16 at 10:35
  • Works perfectly! Thanks. – mnrl Aug 11 '16 at 21:29
  • 1
    This is just so perfect and exactly what I was looking for. Most of people have discussed changing the icon forever for whole app. – Atul Sep 08 '16 at 19:39
  • doesn't work when using `val toolbar = findViewById(R.id.action_bar)` , `toolbar.overflowIcon = Util.getDrawable(R.drawable.ic_menu)`, ` – user25 Feb 02 '19 at 19:16
12

1) Declare menu in your class.

private Menu menu;

2) In onCreateOptionsMenu do the following :

public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_orders_screen, menu);
    return true;
}   

3) In onOptionsItemSelected, get the item and do the changes as required(icon, text, colour, background)

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_search) {
        return true;
    }
    if (id == R.id.ventor_status) {
        return true;
    }
    if (id == R.id.action_settings_online) {
        menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.history_converted));
        menu.getItem(1).setTitle("Online");
        return true;
    }
    if (id == R.id.action_settings_offline) {
        menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.cross));
        menu.getItem(1).setTitle("Offline");
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Note:
If you have 3 menu items :
menu.getItem(0) = 1 item,
menu.getItem(1) = 2 iteam,
menu.getItem(2) = 3 item

Based on this make the changes accordingly as per your requirement.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Surendar D
  • 5,554
  • 4
  • 36
  • 38
  • He's talking about the menu icon itself, not the menu items. – Genc Sep 23 '16 at 09:51
  • 1
    Instead of menu.getItem(0), I recommend using menu.findItem(R.id.viewId) because the for the former become a mess once you have so many items and their position if you make one of the option invisible. – Aman Jan 16 '23 at 13:00
10
//just edit menu.xml file    
//add icon for item which will change default setting icon
//add sub menus


 ///menu.xml file


    <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            android:icon="@drawable/your_icon"
            app:showAsAction="always" >

            <menu>

                <item android:id="@+id/action_menu1"
                    android:icon="@android:drawable/ic_menu_preferences"
                    android:title="menu 1" />

                <item android:id="@+id/action_menu2"
                    android:icon="@android:drawable/ic_menu_help"
                    android:title="menu 2" />

            </menu>
        </item>
Aditya varale
  • 447
  • 8
  • 6
  • Thanks! It works :) Just want to mention that the item of "action_settings" should be wrapped in " ... " – flame3 Jun 27 '16 at 11:00
  • `@string/action_settings` - there must be default android resources for this which includes all languages – user25 Feb 02 '19 at 19:27
5

This works properly in my case:

Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
                                              R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
user7957410
  • 55
  • 1
  • 1
  • doesn't work when using `val toolbar = findViewById(R.id.action_bar)` , `toolbar.overflowIcon = Util.getDrawable(R.drawable.ic_menu)`, ` – user25 Feb 02 '19 at 19:19
4

Use the example of Syed Raza Mehdi and add on the Application theme the name=actionOverflowButtonStyle parameter for compatibility.

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>

    <!-- For compatibility -->
    <item name="actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>

</style>
Joao Ferreira
  • 395
  • 4
  • 9
3

you can achieve this by doing

<item
    android:id="@+id/menus"
    android:actionProviderClass="@android:style/Widget.Holo.ActionButton.Overflow"
    android:icon="@drawable/your_icon"
    android:showAsAction="always">
  <item android:id="@+id/Bugreport"
   android:title="@string/option_bugreport" />

  <item android:id="@+id/Info"
  android:title="@string/option_info" />

 <item android:id="@+id/About"
   android:title="@string/option_about" />
  </item>
Pravin
  • 1,362
  • 12
  • 21
2

this work for me, just set your xml menu like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:icon="@drawable/your_icon"
        android:title="menu"
        app:showAsAction="always">
        <menu>

            <item
                android:id="@+id/action_menu1"
                android:orderInCategory="1"
                android:title="menu 1" />

            <item
                android:id="@+id/action_menu2"
                android:orderInCategory="2"
                android:title="menu 2" />

        </menu>
    </item>
</menu>
IRvanFauziE
  • 823
  • 11
  • 16
1
  1. Change your custom overflow icon of Actionbar in styles.xml

     <resources>
        <!-- Base application theme. -->
        <style name=“MyTheme" parent="@android:style/Theme.Holo">
           <!-- Pointer to Overflow style DONT forget! -->
           <item name="android:actionOverflowButtonStyle">@style/MyOverFlow</item>
        </style>
    
        <!-- Styles -->
        <style name="MyOverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
            <item name="android:src">@drawable/ic_your_action_overflow</item>
        </style>
     </resources>
    
  2. Put custom theme "MyTheme" in application tag of AndroidManifest.xml

    <application
        android:name="com.example.android.MainApp"
        android:theme="@style/AppTheme">
    </application>
    

Have fun.@.@

Luna Kong
  • 3,065
  • 25
  • 20
1

If you want to change icon/title menu in the actionbar/toolbar programmatically,

STEP by STEP

  1. Declare Menu in class

private var mMenu: Menu? = null

  1. Overide onCreateOptionsMenu method and find item so you need to change

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    mMenu = menu
    menuInflater.inflate(R.menu.menu, menu)
    if (mIsFavorite){
        mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable
    } else {
        mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable
    }
    return true
    

    }

  2. Use invalidateOptionsMenu() to update some changes in menu after onCreateOptionsMenu executed. This method will re-create menu

Hai Rom
  • 1,751
  • 16
  • 9
1

Check this out this may work: (in kotlin)

toolBar.menu.getItem(indexNumber).setIcon(R.drawable.ic_myIcon)
Biplob Das
  • 2,818
  • 21
  • 13
0

An short and easy way to change color of option menu index icon is:

<!-- android:textColorSecondary is the color of the menu overflow icon (three vertical dots) -->
<item name="android:textColorSecondary">@color/optionMenuIconColor</item>

Add above line of code into style.xml (custom theme) file, Hope you get answer,Thanks.

AMI CHARADAVA
  • 303
  • 3
  • 9
  • Even if it works, this is a stupid thing to do; it changes the actual secondary app's text color, used in a multitude of places and widgets as a - you guessed it - text color. – Adrian Crețu Mar 22 '17 at 03:12
  • @Adrian Cretu yeah, that might be stupid if you weren't trying to match the text and icon color, globally. Otherwise it seems easy and very reasonable. – me_ Jul 28 '18 at 18:26
  • we're talking about icon, not color, and you answer will change color of many items – user25 Feb 02 '19 at 19:41
0
<?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/logout"
            android:icon="@drawable/logout"
            android:title="Log Out"
            app:showAsAction="always"
        />
</menu>

This did the trick for me!

Palak Jain
  • 664
  • 6
  • 19