Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change ActionBar icon

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?

like image 822
Luca Avatar asked Sep 02 '14 15:09

Luca


4 Answers

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 :)

like image 156
Spurdow Avatar answered Oct 20 '22 05:10

Spurdow


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);
like image 44
Trần Thị Diệu My Avatar answered Oct 20 '22 04:10

Trần Thị Diệu My


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.

like image 24
mysticfyst Avatar answered Oct 20 '22 05:10

mysticfyst


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.

like image 26
Alen Siljak Avatar answered Oct 20 '22 04:10

Alen Siljak