Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change programmatically background color of action bar items

It is easy to set a default color of the items background in action bar by setting:

<item name="android:actionBarItemBackground">@drawable/action_bar_item_background</item>

in application theme.

I would like to change this color only for one of my fragments. How to do it programmatically?

like image 695
Tomasz Avatar asked Aug 01 '14 13:08

Tomasz


People also ask

How can change status bar background color in android programmatically?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.


2 Answers

Have you tried the solution from user3225831 mentioned here: https://stackoverflow.com/a/21297231/1738838

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR")); 
like image 197
Elementary Avatar answered Nov 15 '22 20:11

Elementary


Use simple one line code... Pass RGB values of the required color

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248)));

For Color codes, you can use this website: http://www.rapidtables.com/web/color/RGB_Color.htm

like image 42
Srikanth P Avatar answered Nov 15 '22 20:11

Srikanth P