Skip to content Skip to sidebar Skip to footer

How To Set Status Bar Background As Gradient Color Or A Drawable In Android

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 A

Solution 1:

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)
    publicstaticvoidsetStatusBarGradiant(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Windowwindow = 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:

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

Solution 2:

an extension to the sushant's answer since getColor is deprecated and in kotlin language.

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)funbackGroundColor() {
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)
    window.navigationBarColor = ContextCompat.getColor(this, android.R.color.transparent)
    window.setBackgroundDrawableResource(R.drawable.ic_drawable_vertical_background)
}

The idea is to make transparent status bar and setting a background to the whole window which will also cover the status bar in the same background.

Solution 3:

Here how you do it without any Java code,

Gradient drawable file drawable/bg_toolbar.xml

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:type="linear"android:angle="0"android:startColor="#11998e"android:endColor="#38ef7d" /></shape>

add this in your values/style.xml

<itemname="android:windowBackground">@drawable/bg_toolbar</item><itemname="toolbarStyle">@style/Widget.Toolbar</item><itemname="android:statusBarColor">#00000000</item>

make a new file for your toolbar Gradient values/toolbar.xml

<?xml version="1.0" encoding="utf-8"?><resources><stylename="Widget.Toolbar"parent="@style/Widget.AppCompat.Toolbar"><itemname="contentInsetStart">0dp</item><itemname="android:background">@drawable/bg_toolbar</item></style></resources>

Edit: Add background android:background="#ffffff" in your Activity Layout file.

Solution 4:

Add this code in onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(Color.TRANSPARENT);
android.graphics.drawable.Drawablebackground= MainActivity.this.getResources().getDrawable(R.drawable.status);
getWindow().setBackgroundDrawable(background);

Now add a file with name status in drawable folder and put this code

<layer-listxmlns:android="http://schemas.android.com/apk/res/android" ><item><!-- Edit hex colors as you like --><shapeandroid:shape="rectangle" ><gradientandroid:angle="180"android:endColor="#806AF8"android:startColor="#F16EB5" /></shape></item></layer-list>

Solution 5:

Please take a look of this :

Enter link description here

Use a custom util class to solve this issue

enter image description here

Post a Comment for "How To Set Status Bar Background As Gradient Color Or A Drawable In Android"