How to change option menu icon in the action bar?
AndroidAndroid ActionbarAndroid MenuAndroid OptionsmenuAndroid IconsAndroid Problem Overview
How to change the index icon of option menu?
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:
Android Solutions
Solution 1 - Android
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
Solution 2 - Android
I got a simpler solution which worked perfectly for me :
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Solution 3 - Android
-
Declare
menu
in your class.private Menu menu;
-
In
onCreateOptionsMenu
do the following :public boolean onCreateOptionsMenu(Menu menu) { this.menu = menu; getMenuInflater().inflate(R.menu.menu_orders_screen, menu); return true; }
-
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.
Solution 4 - Android
//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>
Solution 5 - Android
This works properly in my case:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Solution 6 - Android
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>
Solution 7 - Android
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>
Solution 8 - Android
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>
Solution 9 - Android
-
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>
-
Put custom theme "MyTheme" in application tag of AndroidManifest.xml
<application android:name="com.example.android.MainApp" android:theme="@style/AppTheme"> </application>
Have fun.@.@
Solution 10 - Android
If you want to change icon/title menu in the actionbar/toolbar programmatically,
STEP by STEP
-
Declare Menu in class
> private var mMenu: Menu? = null
-
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
}
-
Use invalidateOptionsMenu() to update some changes in menu after onCreateOptionsMenu executed. This method will re-create menu
Solution 11 - Android
> Check this out this may work: (in kotlin) > > toolBar.menu.getItem(indexNumber).setIcon(R.drawable.ic_myIcon)
Solution 12 - Android
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.
Solution 13 - Android
<?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!