Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of android status bar

I want to change the background color of the status bar by writing an application. My android device has black color, I want to change it to some other color. I saw some posts related to it here, but they are telling about notification background.

If any body knows about this please help me.

The default status bar

enter image description here

After using a drawable as background to status bar

enter image description here

like image 211
Yugandhar Babu Avatar asked Jan 28 '12 11:01

Yugandhar Babu


People also ask

How can I customize my Android status bar?

To customize it, first pull down the slider bar from the top of the screen. Next, tap on the three vertical dots in the top right corner. Now click on Status bar. You're in.

How do I change my toolbar color on Android?

Just go to res/values/styles.edit the xml file to change the color of action bar. Code for styles.


5 Answers

In styles.xml do this:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
         <!---Below is the code for status bar color------>
        <item name="android:statusBarColor">@color/color_primary</item>
    </style>
</resources>

Place this is in your values-v21/styles.xml, to enable this on Lollipop and later.

In order to do it programmatically you can do this:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));
like image 55
Gaurav Singh Avatar answered Oct 05 '22 21:10

Gaurav Singh


Sorry, unless you are making a custom ROM this isn't possible, unless you only want the status bar changed for your app.

This would require a heck of a lot of work.

First you will need to add Theme.NoTitleBar.Fullscreen to your manifest

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        >

Then once you have done that you need to create a standard layout which represents the status bar, this would mean that you have to add the time, and also receive all the notifications from other apps, I do not personally know how to do that but I'm sure there is a way.

If you really want to do this goodluck, you have a hard time ahead of you.


Sorry, unless you have the knowledge how to build custom ROMS I do not think this is possible

like image 42
FabianCook Avatar answered Oct 05 '22 22:10

FabianCook


This is possible on Kitkat and after.

If you want to use it in an application (like you asked), you could use this library https://github.com/jgilfelt/SystemBarTint

You only need to write:

// set a custom tint color for all system bars
tintManager.setTintColor(Color.parseColor("#99000FF"));   
// set a custom navigation bar resource
tintManager.setNavigationBarTintResource(R.drawable.my_tint);
// set a custom status bar drawable
tintManager.setStatusBarTintDrawable(MyDrawable);
like image 26
Ravit D Avatar answered Oct 05 '22 23:10

Ravit D


styles.xml was recently changed to themes.xml so most of the answers here are outdated. You can now just simply add this line of code to the themes.xml file to change the status bar color to green:

<item name="android:statusBarColor">@color/black</item>
like image 33
Quessts Avatar answered Oct 05 '22 23:10

Quessts


IF you want to update the status bar color on Lollipop without upgrading your ADT and SDK and all that related stuff, you can use reflections to reach the methods of API 21 (Lollipop) and higher

in your activity :

    if (Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();

        // original code, works on Lollipop SDKs
        // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // window.setStatusBarColor(getResources().getColor(YOUR_COLOR));

        try {
            // to work on old SDKs 
            int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000;
            window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            Class<?> cls = window.getClass();
            Method method = cls.getDeclaredMethod("setStatusBarColor",
                    new Class<?>[] { Integer.TYPE });

            method.invoke(window, Res.color(theme.statusColor));

        } catch (Exception e) {
            // upgrade your SDK and ADT :D
        }

    }

my current minimum API is 15, if you cannot find

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

in your SDK, you can get it's value from documentation as what i did with

WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

hope this helped

like image 35
Ahmed Adel Ismail Avatar answered Oct 05 '22 22:10

Ahmed Adel Ismail