How to Set Status Bar Background as Gradient Color or a Drawable in Android

How to set status bar background as gradient color or a drawable in Android?

Sample ImageFor some one who want to set gradient color to status bar background you can use following method in your activity before setContentView()

For Java

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

For Kotlin

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
fun setStatusBarGradiant(activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val window: Window = activity.window
val background =ContextCompat.getDrawable(activity, R.drawable.gradient_theme)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

window.statusBarColor = ContextCompat.getColor(activity,android.R.color.transparent)
window.navigationBarColor = ContextCompat.getColor(activity,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>

If you want to override status bar with your view then use window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

How to make toolbar and statusbar color gradient from top to bottom

See my answer in Create Linear gradient from top to bottom to status bar and toolbar in android for a detailed explanation.

Taking your code and applying the same process, I get the following code:

res/values/styles.xml:

<resources>

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

</resources>

res/values/styles-v21.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>

</resources>

res/drawable/statusbar_gradient.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

<gradient
android:startColor="#ff008A5B"
android:endColor="#b0008A5B"
android:angle="270"
/>
</shape>

Note the gradient endColor matches the gradient startColor in the following toolbar_gradient.xml.

res/drawable/toolbar_gradient.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#b0008A5B"
android:endColor="#00008A5B"
android:angle="270"
/>
</shape>

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">

<View
android:id="@+id/status_bar_scrim"
android:layout_width="match_parent"
android:layout_height="26dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/my_toolbar"
android:background="@drawable/status_bar_gradient" />

<androidx.appcompat.widget.Toolbar
android:id="@id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/toolbar_gradient"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/status_bar_scrim" />

</androidx.constraintlayout.widget.ConstraintLayout>

Note the use of android:fitsSystemWindows="false" to ensure the scrim overlays the status bar area.

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
// ensure the layout fills the screen
final int layoutFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
window.getDecorView().setSystemUiVisibility(layoutFlags);
// make the status bar translucent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0x00000000);
}
} else {
window.setDecorFitsSystemWindows(false);
}

setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
// set the height of the scrim to standard status bar height for this device (normally 26dp)
View statusBarScrim = findViewById(R.id.status_bar_scrim);
if (statusBarScrim != null) {
int height;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = getResources().getDimensionPixelSize(resourceId);
ViewGroup.LayoutParams lp = statusBarScrim.getLayoutParams();
lp.height = height;
statusBarScrim.setLayoutParams(lp);
statusBarScrim.setVisibility(View.VISIBLE);
}
}
}
}
}

This is what I get:

Example output

N.B. Answer updated to take account of deprecation of WindowManager UI flags in Android 11. The UI flags are still required for versions prior to Android 10 (Q), so it's not possible to completely get rid of deprecation warnings, but they can be guarded by version checks.

Tested successfully on KitKat (19), Marshmallow (23), Nougat (24) and R (30).

How to apply gradient to status bar in android?

Your oncreate should be like this

@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

The Layout File should be like this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="sslwireless.com.testfullscreen.MainActivity">

<!--RePresent Toolbar-->
<LinearLayout
android:orientation="horizontal"
android:background="@drawable/gradient"
android:layout_width="match_parent"
android:layout_height="100dp">

<ImageView
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_back_white_24dp"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp" />

<TextView
android:layout_gravity="center_vertical"
android:textSize="25sp"
android:gravity="center_horizontal"
android:textColor="#fff"
android:text="Test Title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>

And the gradient file looks like this .

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="135" android:startColor="#f56f2c" android:endColor="#fa9f46"/>
</shape>

The ui will be look like this .

Sample Image

Create Linear gradient from top to bottom to status bar and toolbar in android

On KitKat and later you can control the status bar and action bar backgrounds by setting the status bar as translucent and using a full-screen layout with a scrim, while taking care to blend gradients from status bar into action bar.

One caveat is that KitKat uses its own gradient scrim on top of our own and that doesn't appear to be possible to override, but on Lollipop+ there is no such problem.

Here is an example of the working activity on a MarshMallow emulator:

Example output

And here is the code I used:

res/values/styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="background">@drawable/actionbar_bg</item>
</style>

<style name="AppTheme.ActionBar.Popup" parent="ThemeOverlay.AppCompat">
<item name="android:colorBackground">@color/colorPrimaryDark</item>
<item name="background">@color/colorPrimaryDark</item>
</style>

res/values/colors.xml:

<resources>
<color name="colorPrimary">#ff7000e0</color>
<color name="colorPrimaryDark">#ff400080</color>
<color name="colorAccent">#ff40aba0</color>
</resources>

res/drawable/statusbar_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#ff7000e0"
android:startColor="#ff8000ff" />
</shape>

res/drawable/actionbar_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ff7000e0"
android:endColor="#ff400080"
android:angle="270" />
</shape>

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="false" >

<View
android:id="@+id/main_activityStatusBarScrim"
android:layout_width="match_parent"
android:layout_height="26dp"
android:background="@drawable/statusbar_bg"/>

<android.support.v7.widget.Toolbar
android:id="@+id/main_activityToolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@drawable/actionbar_bg"
app:popupTheme="@style/AppTheme.ActionBar.Popup"/>

<FrameLayout
android:id="@+id/main_activityFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ffffffff">

</FrameLayout>

</LinearLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// make the main layout fill the screen
Window window = getWindow();
final int layoutFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
window.getDecorView().setSystemUiVisibility(layoutFlags);
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

// make the status bar translucent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0x00000000);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// N.B. on some (most?) KitKat devices the status bar has a pre-defined gradient that
// cannot be overridden.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

// inflate main layout and set up action bar
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.main_activityToolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}

// set the height of the scrim to standard status bar height for this device (normally 26dp)
View statusBarScrim = findViewById(R.id.main_activityStatusBarScrim);
int height;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = getResources().getDimensionPixelSize(resourceId);
} else {
// belt and braces
height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 26f, getResources().getDisplayMetrics());
}
if (statusBarScrim != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup.LayoutParams lp = statusBarScrim.getLayoutParams();
lp.height = height;
statusBarScrim.setLayoutParams(lp);
statusBarScrim.setVisibility(View.VISIBLE);
}
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
...


Related Topics



Leave a reply



Submit