Vishal
Vishal

Reputation: 49

How to manage status bar color?

I am android beginner , I want to create status bar as same as Toolbar and when DrawerLayout is opened statusBar again change as same as DrawerLayout ,how to solve this problem . I have already set in style parent="Theme.AppCompat.Light.NoActionBar

<android.support.v4.widget.DrawerLayout
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"
tools:context=".MainActivity"
android:background="#ed9797"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/DrawerLayout">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="#e62e2e"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


    <ListView
        android:background="#46dfcd"
        android:id="@+id/leftDrawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:text="Drawer page"
        android:layout_gravity="start"/>

Upvotes: 1

Views: 271

Answers (3)

SachinS
SachinS

Reputation: 2253

To set a custom color for the status bar, use the android:statusBarColor attribute on your application theme under styles.xml like

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your status bar here. -->
    <item name="android:statusBarColor">@color/red</item>

    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Upvotes: 1

Pankaj
Pankaj

Reputation: 8058

You can do like this:

     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(getResources().getColor(
                                R.color.your_color));
            }
           setContentView(R.layout.your_layout);

                   //Your implementation
     }

Upvotes: 0

Anshuman Borah
Anshuman Borah

Reputation: 538

To change status bar color use setStatusBarColor(int color).

Working snippet of code:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));

Upvotes: 1

Related Questions