Search code examples
androidmaterial-designstatusbar

Android - Change status bar color on pre-lollipop devices programatically


I want to change the status bar colour on pre-lollipop devices programmatically. I am well aware the material design colouPrimaryDark wont work on pre-lollipop as status bar colour is concern of OS itself which pre-lollipop devices wont provide such feature. So I want to do it programmatically through java file. Is that possible?

currently I am using this material design code.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColor</item>
    <item name="colorAccent">@color/primaryColor</item>
</style>

As this wont work with API below 21. So I want to do it through java.


Solution

  • Actually, we can use that on + KitKat.

    Check this link: http://developer.android.com/reference/android/R.attr.html#windowTranslucentStatus

    Just add this to the top of your layout:

    <FrameLayout
            android:id="@+id/statusbar"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:background="@color/colorPrimaryDark" />
    

    And use this:

    if(Build.VERSION.SDK_INT == 19) {
    
                FrameLayout statusbar = (FrameLayout) findViewById(R.id.statusbar);
                statusbar.setVisibility(View.GONE);
            }
    

    It should work on Kitkat, and like i said, this is also available only for + Kitkat.