statusbar – How to change the background color of android status bar-ThrowExceptions

Exception or error:

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

How to solve:

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

###

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

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *