Reputation: 14404
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:
<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" />
Upvotes: 57
Views: 117997
Reputation: 683
<?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!
Upvotes: 0
Reputation: 3114
Check this out this may work: (in kotlin)
toolBar.menu.getItem(indexNumber).setIcon(R.drawable.ic_myIcon)
Upvotes: 1
Reputation: 852
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>
Upvotes: 3
Reputation: 1811
If you want to change icon/title menu in the actionbar/toolbar programmatically,
STEP by STEP
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
Upvotes: 1
Reputation: 65
This works properly in my case:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Upvotes: 6
Reputation: 313
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.
Upvotes: 0
Reputation: 4117
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
Upvotes: 82
Reputation: 5644
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.
Upvotes: 12
Reputation: 405
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>
Upvotes: 4
Reputation: 467
//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>
Upvotes: 12
Reputation: 3971
I got a simpler solution which worked perfectly for me :
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Upvotes: 65
Reputation: 3155
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.@.@
Upvotes: 1
Reputation: 1362
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>
Upvotes: 3