how to set status bar background as gradient color or a drawable in android
For 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>
an extension to the sushant's answer since getColor is deprecated and in kotlin language.
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)fun backGroundColor() { 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.
Here how you do it without any Java code,
Gradient drawable file drawable/bg_toolbar.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:type="linear" android:angle="0" android:startColor="#11998e" android:endColor="#38ef7d" /></shape>
add this in your values/style.xml
<item name="android:windowBackground">@drawable/bg_toolbar</item><item name="toolbarStyle">@style/Widget.Toolbar</item><item name="android:statusBarColor">#00000000</item>
make a new file for your toolbar Gradient values/toolbar.xml
<?xml version="1.0" encoding="utf-8"?><resources> <style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar"> <item name="contentInsetStart">0dp</item> <item name="android:background">@drawable/bg_toolbar</item> </style></resources>
Edit: Add background android:background="#ffffff"
in your Activity Layout file.