How to change option menu icon in the action bar?
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.
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
I got a simpler solution which worked perfectly for me :
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);