how to set status bar background as gradient color or a drawable in android

sushant gosavi  picture sushant gosavi · Mar 31, 2017 · Viewed 29.5k times · Source

I want to set status bar background as gradient theme also status bar and action bar color should same gradient drawable, as per documentation we can set color to status bar in API level 21 and above by using

<item name="android:statusBarColor">@color/colorPrimary</item>

But i am searching something like

<item name="android:statusBarDrawable">@drawable/myDrawable</item>

I have seen example that use

 <item name="android:windowTranslucentStatus">false</item>
   <item name="android:windowTranslucentNavigation">false</item>

but in that case status bar and action bar overlap (use fitSystemWindow =true but still not solved) also try with https://github.com/jgilfelt/SystemBarTint this library but still no luck

Thank you in advance!!

Answer

sushant gosavi  picture sushant gosavi · Apr 14, 2017

enter image description hereFor some one who want to set gradient color to status bar background you can use following method in your activity before setContentView()

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setStatusBarGradiant(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            Drawable background = activity.getResources().getDrawable(R.drawable.gradient_theme);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
            window.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
            window.setBackgroundDrawable(background);
        }
    } 

Thanks every one for your help

EDIT

If the above code don't work, try to add this in your styles.xml:

<style name="AppTheme.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>