Programmatically change ActionBar icon

16,981

Solution 1

try this one

mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);

Assuming you have it all set 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 it helps :)

Solution 2

I hope it will be help for 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);

Solution 3

You have to modify your 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 that i created which returns a drawable. So all you need to do is replace getTintedDrawable(R.drawable.search, R.color.blue) by your drawable.

NOTE: I have used menu.getItem(0) my code since I had only 1 item defined in menu/items.xml. If you have multiple try different values (from 0 to one less than number of menu items). My guess would be that its the number at which the item is defined but I'm not too sure.

Solution 4

I manage to rotate/change the icon this way:

MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);
<prepare the image view from drawable here>
item.setActionView(imageView);

Seems to work OK. You could also simply use the item.setIcon() instead.

Share:
16,981
Luca
Author by

Luca

Android lover and developer

Updated on June 16, 2022

Comments

  • Luca
    Luca almost 2 years

    I'm coming back to my main activity from a fragment and for some logic I have to change the appearence of an icon on the action bar menu.

    This is the menu on the action bar:

    <?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 have to change the sencond item (the one with id = action_filter). I've tried different solutions, found on different post. The most rated is

    mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
    

    but seems not to work.

    On the other side getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter)); changes the logo, and I don't want so.

    How can i change only the second item on menu?