Robert Golusiński
Robert Golusiński

Reputation: 358

Status bar is changing color to the 'windowBackground' instead of using 'colorPrimaryDark'

I'm styling my android mobile app using AppCompat. It's running android 6.0 which is API 23.

Here is my AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="FeedingTime.FeedingTime" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk />
    <application android:icon="@drawable/Icon" android:label="Feeding Time" android:theme="@style/AppTheme">
    </application>
</manifest>

Here is my style.xml:

  <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowTranslucentStatus">true</item>

    <item name="android:windowBackground">@color/background_color</item>

    <item name="colorPrimary">#6497b1</item>
    <item name="colorPrimaryDark">#005b96</item>
  </style>

Here is the XML of the Activity layout where the problem exist:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
  <include
        android:id="@+id/toolbarHistoryActivity"
        layout="@layout/toolbar" />
  <ListView
      android:minWidth="25px"
      android:minHeight="25px"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="4"
      android:id="@+id/listViewHistory" />
  <Button
      android:text="Clear History"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:id="@+id/btnClearHistory" />
</LinearLayout>

I'm running into a problem, in home screen activity the status bar color is set to dark blue as expected, but when i open the second activity my status bar changes color, it's no longer using the 'colorPrimaryDark' but rather a darker version of the color i have in 'windowBackground'.

Why is that?

Upvotes: 0

Views: 875

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

Add a Coordinatorlayout as root and use android:fitsSystemWindows="true"

instead of Linearlayout

<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">

you can also programetically set StatusBar color in your Activity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.your_color));  //change color here
}

Upvotes: 2

Related Questions