Change ActionBar icon programmatically
I am returning to my main activity from a fragment and by some logic I should change the appearance of the icon in the action bar menu.
This is the action bar menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gn.sfa.Main">
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="Search" />
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_action_filter_empty"
android:showAsAction="ifRoom"
android:title="Filter" />
<item
android:id="@+id/action_new"
android:icon="@drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="New" />
</menu>
I need to change the sencond element (the one with id = action_filter
). I have tried different solutions found on different posts. Most rated
mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
but doesn't seem to work.
On the other hand it getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
changes the logo and I don't want that.
How can I only change the second item in the menu?
source to share
try this
mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);
Assuming you set everything up for mOptionsMenu
in
private Menu mOptionsMenu;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// inflating your menu here
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Hope this helps :)
source to share
Hope this helps you.
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>" + "Messages" + "</font>"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.messagebar_color)));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arrow_black);
source to share
You need to change onCreateOptionsMenu (menu menu)
I changed the color of my search bar programmatically. I am posting the code here. Hope it helps.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
menu.getItem(0).setIcon(getTintedDrawable(R.drawable.search, R.color.blue));
return super.onCreateOptionsMenu(menu);
}
Where getTintedDrawable () is a function I created that returns drawable. So all you have to do is replace getTintedDrawable(R.drawable.search, R.color.blue)
with your drawable.
NOTE. I used menu.getItem(0)
my code as I only had 1 item defined in the /items.xml menu. If you have several attempts at different values (from 0 to one less than the number of menu items). I am assuming this is the number the item is defined in, but I'm not sure.
source to share