Reputation: 72
I'm trying to change the StatusBar
color programatically with no success. I'm using the MaterialDrawer library to create the nav drawer btw.
Tried differents solutions I found in internet but no one worked, the statusbar always show the primaryDark
Tried programatically:
if (Build.VERSION.SDK_INT >= 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(Color.BLUE);
}
Also tried with themes:
if (dark)
setTheme(R.style.AppTheme_Dark_NoActionBar);
else
setTheme(R.style.AppTheme_NoActionBar);
<style name="AppTheme.Dark.NoActionBar" parent="MaterialDrawerTheme">
<item name="android:colorPrimary" tools:targetApi="lollipop">#ffffff</item>
<item name="android:colorPrimaryDark" tools:targetApi="lollipop">#cfcfcf</item>
<item name="android:statusBarColor">#cfcfcf</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/md_black_1000</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="drawerArrowStyle">@style/MaterialDrawer.DrawerArrowStyle.Black</item>
<item name="android:datePickerDialogTheme">@style/DatePickerDialogThemeDark</item>
<item name="android:timePickerDialogTheme">@style/DatePickerDialogThemeDark</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/primary</item>
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:datePickerDialogTheme">@style/DatePickerDialogThemeLight</item>
<item name="android:timePickerDialogTheme">@style/DatePickerDialogThemeLight</item>
</style>
<style name="AppTheme" parent="MaterialDrawerTheme.Light">
<item name="android:colorPrimary" tools:targetApi="lollipop">@color/colorPrimary</item>
<item name="android:colorPrimaryDark" tools:targetApi="lollipop">@color/colorPrimaryDark
</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/md_blue_300</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
<item name="drawerArrowStyle">@style/MaterialDrawer.DrawerArrowStyle</item>
</style>
And finally tried using this piece of code of MaterialDrawer library:
drawer.getDrawerLayout().setStatusBarBackgroundColor(color);
drawer
is the result of DrawerBuilder
. The app has 2 primary colors depending on some variables the user put in the login/register. This colors are blue and white. The app works nice, except for that the StatusBar only is tinted with primaryDark even if I use the codes I posted above. The only mode I can change the color is to modify the colorPrimary or putting this into colors:
<color name="materialize_primary_dark">@color/md_red_500</color>
But it changes the color as primaryDark do, and I have to change it to white for white mode and blue for blue mode.
Thanks.
Upvotes: 1
Views: 319
Reputation: 2618
You not need library for that use this style-v21
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
Drawer layout
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true" /><--This is mandatory
</android.support.v4.widget.DrawerLayout>
Or you can refer my accepted answer here Android DrawerLayout (with NavigationView) behind status bar
Upvotes: 1
Reputation: 12868
As soon as you have a DrawerLayout
in the root of your View
which has the fitsSystemWindows
flag, the StatusBar color will be managed by this views "scrim"
.
So in your case you can do the following to adjust the StatusBar
's color dynamically:
public void setStatusBarColor(@ColorInt int color){
drawer.getDrawerLayout().setStatusBarBackgroundColor(color);
}
Upvotes: 2