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?

+3


source to share


4 answers


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

+15


source


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

      

+3


source


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.

+1


source


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 fine. You can also just use item.setIcon()

.

0


source







All Articles