Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ActionBar Background Color in code from Navigation List

I want to change the color of the Action Bar background when the user chooses a selection in the Navigation List.

Currently, my code looks like this:

@Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        ColorDrawable colorDrawable = new ColorDrawable();
        ActionBar actionBar = getActionBar();
        if(itemPosition == 0)
        {
            colorDrawable.setColor(0xffFEBB31);
            actionBar.setBackgroundDrawable(colorDrawable);
            return true;
        }
        if(itemPosition == 1)
        {
            colorDrawable.setColor(0xff9ACC00);
            actionBar.setBackgroundDrawable(colorDrawable);
            return true;
        }
        return false;
    }

However, the first time I select itemPosition 1 in the Navigation List, it changes the ActionBar color to white.

enter image description here
The second time I click the itemPosition 1 in the Navigation List, I have no issue.

enter image description here
Could anyone tell me why this is and how I can fix the problem? Thank you for the help!

like image 839
Tykin Avatar asked Aug 02 '12 03:08

Tykin


2 Answers

Try this:

myActivity.invalidateOptionsMenu();
like image 28
user2426991 Avatar answered Oct 06 '22 00:10

user2426991


Try using this code:

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
actionBar.setBackgroundDrawable(colorDrawable); 
like image 87
Trevor Avatar answered Oct 05 '22 22:10

Trevor