I tried to color my bottom navigation bar
In some android versions it's look white background with white icons
protected void updateNavigationBarColor(Window window, boolean isLight) {
if (window == null) {
return;
}
View decorView = window.getDecorView();
if (decorView == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
window.setNavigationBarColor(Utils.getColor(R.color.navigation_bar_color));//black or white
WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(window, decorView);
controller.setAppearanceLightNavigationBars(isLight);
} else {
// Do nothing
}
}
The navigation bar background color is exactly what I ask for. but the 3 buttons (icons) are stay in always light
In themes.xml added to my theme
<item name="android:navigationBarColor">#color/transparent</item>
<item name="android:windowLightNavigationBar">true</item>
Related
I've exhausted essentially every post I've found regarding changing the status bar color programmatically, but none of them touch on this specific case.
I have an Activity that contains a ViewPager and a BottomNavigationView. The ViewPager holds three Fragments and the BottomNavigationView moves between them with smoothscroll turned off.
As the first of the three Fragments is a map, I'd like the status bar to be transparent when the first Fragment is shown, but revert back to its opaque color on the other two Fragments.
Here's some code that shows how I'm attempting to set and revert the status bar states.
private class TabSelectedObserver implements Observer<Integer> {
#Override
public void onChanged(#Nullable Integer selectedTab) {
if (selectedTab != null) {
activityMainBinding.mainPager.setCurrentItem(selectedTab, false);
switch (selectedTab) {
case 0:
applyTransparentStatusBarTheme();
break;
default:
applyOpaqueStatusBarTheme();
break;
}
}
}
}
private void applyTransparentStatusBarTheme() {
hideSystemUi();
getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
private void applyOpaqueStatusBarTheme() {
showSystemUi();
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.rippallete_700));
}
private void hideSystemUi() {
// Set flags for hiding status bar and navigation bar
mSystemUiVisibility = mSystemUiVisibility
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}
private void showSystemUi() {
// Reset flags for hiding status bar and navigation bar
mSystemUiVisibility = mSystemUiVisibility
& ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}
Here's a gif showing the behavior I end up with
Every thing I tried with fitSystemWindows didn't produce the result I wanted.
If anyone has any suggestions for how you would give each Fragment an individual status bar color, with 1 being transparent, I'd really appreciate it.
So assuming you're setting the status bar colours based on the position of whatever tab you have selected you could do something similar to this:
private void updateStatusBarColour(int tabPosition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
switch (tabPosition) {
case FIRST_TAB:
case SECOND_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryLight));
break;
case THIRD_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(0);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
break;
case FOURTH_TAB:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mRootView.setSystemUiVisibility(0);
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorDarkTan));
break;
}
}
}
Note that I call setSystemUiVisibility on mRootView, where mRootView is the root parent view containing the ViewPager (or whatever is the root layout of your containing Activity). The call specifically sets the theme of the status bar so that status bar items (like the battery symbol) can be visible on dark or light background depending on what colour you end up setting.
In the Android M we have ability to make status bar icons dark. To do that we can specify attribute in the theme's xml:
<item name="android:windowLightStatusBar">true</item>
OR we cat set it at runtime with this code:
View someView = findViewById(R.id.some_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
someView.setSystemUiVisibility(someView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
And it actually works fine. But question is how to properly set a status bar mode to dark at runtime?
I already tried these variants:
// Makes status bar mode dark, but also hides it along with all navigation views.
someView.setSystemUiVisibility(someView.getSystemUiVisibility() | ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
// Does nothing
someView.setSystemUiVisibility(someView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
// Also does nothing
someView.setSystemUiVisibility(someView.getSystemUiVisibility() ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
So how it can be done the right way?
The solution posted by #Aracem is valid but, doesn't work if you try change also the background color of the status bar. In my case I do it in the following way.
To enable windowLightStatusBar(programatically,inside a Utils class for example):
public static void setLightStatusBar(View view,Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
activity.getWindow().setStatusBarColor(Color.WHITE);
}
}
To restore to StatusBar to the previous state:
public static void clearLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = activity.getWindow();
window.setStatusBarColor(ContextCompat
.getColor(activity,R.color.colorPrimaryDark));
}
}
Restoring the color of the status bar is enough, it restores also the icons colors.
VERY IMPORTANT: The restore operation will not occur until the view used in setLightStatusBar(View view..) dissapears(that is, view.getVisibility()==GONE|INVISIBLE) from the screen.
According to Nick Butcher's project "Plaid"
public static void clearLightStatusBar(#NonNull View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
}
}
You can find this file here.
I base on #Aracem and #Carlos Hernández Gil but I think it will easy to understand if we use bitwise XOR (^ operator in Java)
private void setLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // add LIGHT_STATUS_BAR to flag
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
activity.getWindow().setStatusBarColor(Color.GRAY); // optional
}
}
private void clearLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // use XOR here for remove LIGHT_STATUS_BAR from flags
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
activity.getWindow().setStatusBarColor(Color.GREEN); // optional
}
}
Explain
First, look at SYSTEM_UI_FLAG_LIGHT_STATUS_BAR and setSystemUiVisibility
/**
* Flag for {#link #setSystemUiVisibility(int)}: Requests the status bar to draw in a mode that
* is compatible with light status bar backgrounds.
*/
public static final int SYSTEM_UI_FLAG_LIGHT_STATUS_BAR = 0x00002000;
public void setSystemUiVisibility(int visibility) {
if (visibility != mSystemUiVisibility) {
mSystemUiVisibility = visibility;
...
}
}
I think 2 lines code below is quite hard to understand
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for set light status bar
flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for clear light status bar
At first look, I just think we can use simple like
flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for set light status bar
flags = 0; // for clear light status bar (0 <=> LIGHT_STATUS_BAR <=> default systemUiVisibility)
But we should use | and ^ because
Example, we want to set both status bar and navigationbar to light, then we will use
flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
When we don't want status bar is light anymore, we can use
flags = View.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
OR
flags = activity.getWindow().getDecorView().getSystemUiVisibility();
flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
To know more why we use | and ^, I think the tutorial below may help
https://medium.com/#JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1
Here is my understand. Hope this help
The way I switched light and dark for APIs 23-30 was a little different than these. This is a kotlin version
Since I was using Compose with the Crossfade animation to change themes, in some cases would call this function twice, hence making xor undo itself. An alternative is an inverse or operation. My light theme switcher-thing ended up looking like this
#Suppress("DEPRECATION")
fun invertInsets(darkTheme: Boolean, window: Window) {
if (Build.VERSION.SDK_INT >= 30) {
//Correct way of doing things
val statusBar = APPEARANCE_LIGHT_STATUS_BARS
val navBar = APPEARANCE_LIGHT_NAVIGATION_BARS
if (!darkTheme) {
window.insetsController?.setSystemBarsAppearance(statusBar, statusBar)
window.insetsController?.setSystemBarsAppearance(navBar, navBar)
} else {
window.insetsController?.setSystemBarsAppearance(0, statusBar)
window.insetsController?.setSystemBarsAppearance(0, navBar)
}
} else {
// Does bitwise operations (or to add, inverse or to remove)
// This is depreciated but the new version is API 30+ so I should have this here
val flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
if (Build.VERSION.SDK_INT >= 26) View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR else 0
if (!darkTheme) {
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility or flags
} else {
window.decorView.systemUiVisibility =
(window.decorView.systemUiVisibility.inv() or flags).inv()
}
}
}
The bit for API 30+ is what's not depreciated, but realistically not many phones are at API 30 so there's also the bit for lower APIs
It just calculates flags (since setting LIGHT_NAVIGATION_BARS is API 26+) beforehand for conciseness and then either definitively sets or resets those exact flags. No and or xor funny buisiness. or will always set the flags to 1, and the inverse or thing will always set the flags to 0. This is only possible because both SYSTEM_UI_FLAG_LIGHT_STATUS_BAR and SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR are one bit, however. Otherwise it would probably need to use xor.
systemUiVisibility - is deprecated now. You can use WindowInsetsControllerCompat instead.
private val insetsController: WindowInsetsControllerCompat? by lazy {
activity?.window?.let { window -> WindowInsetsControllerCompat(window, window.decorView) }
}
private fun setLightStatusBar(light: Boolean) {
insetsController?.isAppearanceLightStatusBars = light
}
UPD:
Above constructor for WindowInsetsControllerCompat is deprecated, so use the following instantiation instead:
private val insetsController: WindowInsetsControllerCompat? by lazy {
activity?.window?.decorView?.let(ViewCompat::getWindowInsetsController)
}
There is a slight change in API 30 of the SDK and now the light status bar appearance is controlled by WindowInsetsController, which can be obtained from a Window.
Below is a sample method (within an Activity) in Kotlin, combining the new API with the previously used View.setSystemUiVisibility for older Android SDK versions. Bear in mind that this only changes the system icons appearance of the status bar and the actual color of the status bar can still be set by Window.setStatusBarColor.
#Suppress("DEPRECATION")
private fun setSystemUiLightStatusBar(isLightStatusBar: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val systemUiAppearance = if (isLightStatusBar) {
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
} else {
0
}
window.insetsController?.setSystemBarsAppearance(systemUiAppearance,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
} else {
val systemUiVisibilityFlags = if (isLightStatusBar) {
window.decorView.systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
window.decorView.systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
window.decorView.systemUiVisibility = systemUiVisibilityFlags
}
}
}
I put together this simple utility object that allows you to change status bar color and light status bar on/off for within any fragment. However, this relies on using the Android Jetpack Navigation component for navigation (Kotlin):
object StatusBarUtil {
fun changeStatusBarColor(activity: Activity, #ColorInt color: Int, lightStatusBar: Boolean) {
activity.window?.let { win ->
val nav = Navigation.findNavController(activity, R.id.your_nav_host_fragmen /* TODO: Use the ID of your nav host fragment */)
val currentDest = nav.currentDestination?.id
val oldColor = win.statusBarColor
val oldFlags = win.decorView.systemUiVisibility
win.statusBarColor = color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
var flags = oldFlags
flags = if (lightStatusBar) {
flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
win.decorView.systemUiVisibility = flags
}
nav.addOnNavigatedListener { _, dest ->
if (dest.id != currentDest) {
win.statusBarColor = oldColor
win.decorView.systemUiVisibility = oldFlags
}
}
}
}
}
To use this, call the following from within any fragment's onViewCreated:
StatusBarUtil.changeStatusBarColor(requireActivity(), someDarkColor, false)
Based on #phan-van-linh answer, I wrote this class for Xamarin Android
public static class ActivityExtensions
{
public static void SetLightStatusBar(this Activity activity)
{
int flags = (int)activity.Window.DecorView.SystemUiVisibility; // get current flag
flags |= (int)SystemUiFlags.LightStatusBar; // add LIGHT_STATUS_BAR to flag
activity.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;
//activity.Window.SetStatusBarColor(Color.GRAY); // optional
}
public static void ClearLightStatusBar(this Activity activity)
{
int flags = (int)activity.Window.DecorView.SystemUiVisibility; // get current flag
flags = flags ^ (int)SystemUiFlags.LightStatusBar; // use XOR here for remove LIGHT_STATUS_BAR from flags
activity.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;
//activity.Window.setStatusBarColor(Color.GREEN); // optional
}
}
To change to light status bar use:-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
activity?.window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
To change back to dark status bar :-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
activity?.window?.decorView?.systemUiVisibility = 0
i will make some changes in above answers.
make a class
public class DarkStatusBar {
public static void setLightStatusBar(View view, Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
activity.getWindow().setStatusBarColor(Color.WHITE);
}
}
}
and Call it wherever you want like this
Window window = getWindow();
View view = window.getDecorView();
DarkStatusBar.setLightStatusBar(view,this);
It works for me
fun Activity.clearLightStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val window = window
window.statusBarColor = ContextCompat
.getColor(this, R.color.ultramarine_blue)
}
}
Set blue background status bar with light text color kotlin version
fun setBlueStatusBarColor(window: Window, context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.statusBarColor = context.getColor(R.color.colorBlue)
}else {
window.statusBarColor = context.resources.getColor(R.color.colorBlue)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
var flags: Int = window.decorView.systemUiVisibility
flags = flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.decorView.systemUiVisibility = flags
}
}
}
/**
* Changes color of the status bar icons
* #param isLight if true - shows dark icons, light else
*/
fun setStatusBarUiTheme(activity: Activity?, isLight: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity?.window?.decorView?.let {
it.systemUiVisibility = if (isLight)
it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR // dark icons
else
it.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() // light icons
}
}
}
In res/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowLightStatusBar">true</item>
.......
</style>
<style name="AppTheme.DarkStatus" parent="AppTheme" tools:targetApi="23" >
<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor" >#color/status_bar_color</item>
</style>
In code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme_DarkStatus); //To set DarkStatusBar theme
setContentView(R.layout.activity_drawer);
....
}
For people who doesn't have a Window instance. It's also possible to do with a View instance (For API 30) :
fun setLightStatusBar(view: View) = view.windowInsetsController?.setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
fun clearLightStatusBar(view: View) = view.windowInsetsController?.setSystemBarsAppearance(
0,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
How to set status bar color dynamically for an application, am using view pager while swiping (horizontally) status bar color and title bar and button should change the color . as per my code title and button color changing perfectly ,but the issue is status bar color taking next color from array list. how to fix that issue can anyone help me. here is my code
private int[] colors = new int[]{0xffffd200, 0xff37beb7, 0xff00ccff, 0xff8585c1, 0xfff2a03c, 0xff2a80b9, 0xfff15972,
0xffe9776c, 0xff9dcc96,0xff76c069};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = ((Activity) context).getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
int coloring = position % colors.length;
int new_color = colors[coloring];
window.setStatusBarColor(new_color);
title_bar.setBackgroundColor(new_color);
set_share.setBackgroundColor(new_color);
}
else{
int color = position % colors.length;
itemView.setBackgroundColor(colors[color]);
title_bar.setBackgroundColor(colors[color]);
set_share.setBackgroundColor(colors[color]);
}
To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.
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));
This is taken from following reference:
How to change status bar color to match app in Lollipop? [Android]
coming to Status bar color, you can only add it to devices with API level more than 21. To those devices which satisfy this condition you can change the StatusBar color dynamically as shown below.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
}
When I wanted to set the status bar color, I used https://github.com/jgilfelt/SystemBarTint
I used it like that :
public static void colorStatusBar(Window window, Activity activity) {
Log.v(Constants.LOG_TAG, "Start defining color bar status");
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
}
}
But beware that setting status bar color is only possible if your app runs on a phone with API >= 19
I want to be able to set the color of a progress bar so that users can easily determine if the bar is indicating completion or almost completed. This is illustrated in the screenshot below and works fine in Lollipop (API 21).
The code that works is
private void initializeOriginalStyles(View view) {
if (!mOriginalStylesSet) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.personEventProgressBar);
if (progressBar != null) {
mProgressBarColour = progressBar.getProgressDrawable().getColorFilter();
}
}
mOriginalStylesSet = true;
}
}
and in the Adapter.bindView() I set the progress drawable like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (numSkillsInLevelForEvent == numSkillsCompletedInLevel) {
progressBar.getProgressDrawable().setColorFilter(mContext.getResources().getColor(R.color.pass_level), PorterDuff.Mode.SRC_IN);
} else {
progressBar.getProgressDrawable().setColorFilter(mProgressBarColour);
}
}
This is because in APi 21 we have access to Drawable.getColorFilter() and so I can capture the original color filter and set it when progress is not completed.
Setting the color is no problem because Drawable.setColorFilter() has been available since API 1. This means prior to API 21 I can set it to my color which indicates completion but don't know what to set it back to.
Trying to work around this API limitation I have tried setting the progress bar drawable and calling Drawable.mutate() before setting the color to my custom color which indicates completion. Unfortunately this does not work as expected and the color for all progress bars is tinted to the completion color which means that the attribute is shared amongst all progress bars and that mutate isn't doing what I expect.
Here is the code where I try to capture the original progress bar drawable
private void initializeOriginalStyles(View view) {
if (!mOriginalStylesSet) {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.personEventProgressBar);
if(progressBar != null) {
mProgressBarDrawable = progressBar.getProgressDrawable();
}
mOriginalStylesSet = true;
}
}
and in the Adapter.bindView() method I try to set the progress drawable like this
if(numSkillsInLevelForEvent == numSkillsCompletedInLevel) {
Drawable completedProgressBar = mProgressBarDrawable.mutate();
completedProgressBar.setColorFilter(mContext.getResources().getColor(R.color.pass_level), PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(completedProgressBar);
} else {
progressBar.setProgressDrawable(mProgressBarDrawable);
}
This all results in the progress bars all being set to the completed color
Any ideas on how I can accomplish this?
Thanks!
I'm developing an simple app to test the material design. I'm using com.android.support:appcompat-v7:21.0.0 and my activity looks like:
public class MyActivity extends ActionBarActivity {
...
}
The layout is defined as:
<LinearLayout 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=".MyActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="128dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"/>
</LinearLayout>
Now i defined my theme following material guidelines:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary500</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark700</item>
</style>
I'd like to change the status bar color in pre Android 5 and set it to colorPrimaryDark but i can't find the way. I tried using:
getWindow().setStatusBarColor(..)
but setStatusBar color is available from level 21.
Why if i define a colorPrimaryDark in my theme and use appcompact the status bar doesn't change color?
Anyone can help?
While colouring the status bar is not supported <5.0, on 4.4 you can use a work around to achieve a darker colour:
Make the status bar translucent
<item name="android:windowTranslucentStatus">true</item>
Then use AppCompat's Toolbar for your appbar, making sure that it fits system windows:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
...
android:fitsSystemWindows="true"/>
Make sure to set your toolbar as your activity's toolbar:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The toolbar stretches underneath the status bar, and the semi translucency of the status bar makes it appear to be a darker secondary colour. If that's not the colour you want, this combination allows you to fit a view underneath your status bar sporting the background colour of your choice (though it's still tinted darker by the status bar).
Kind of an edge case workaround due to 4.4 only, but there ya go.
The status bar is a system window owned by the operating system. On pre-5.0 Android devices, applications do not have permission to alter its color, so this is not something that the AppCompat library can support for older platform versions. The best AppCompat can do is provide support for coloring the ActionBar and other common UI widgets within the application.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.actionbar));
}
Put this code in your Activity's onCreate method. This helped me.
As others have also mentioned, this can be readily solved by adding the following to the onCreate() of the Activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
}
However, the important point I want to add here is that in some cases, even the above does not change the status bar color. For example, when using MikePenz library for Navigation Drawer, it implicityly overrides the status bar color, so that you need to manually add the following for it to work:
.withStatusBarColorRes(R.color.status_bar_color)
Status bar coloring is not supported in AppCompat v7:21.0.0.
From the Android developers blog post
On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets.
This means the AppCompat lib will only color status bars on Lollipop and above.
Switch to AppCompatActivity and add a 25 dp paddingTop on the toolbar and turn on
<item name="android:windowTranslucentStatus">true</item>
Then, the will toolbar go up top the top
This solution sets the statusbar color of Lollipop, Kitkat and some pre Lollipop devices (Samsung and Sony).
The SystemBarTintManager is managing the Kitkat devices ;)
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hackStatusBarColor(this, R.color.primary_dark);
}
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
public static View hackStatusBarColor( final Activity act, final int colorResID ) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
if (act.getWindow() != null) {
final ViewGroup vg = (ViewGroup) act.getWindow().getDecorView();
if (vg.getParent() == null && applyColoredStatusBar(act, colorResID)) {
final View statusBar = new View(act);
vg.post(new Runnable() {
#Override
public void run() {
int statusBarHeight = (int) Math.ceil(25 * vg.getContext().getResources().getDisplayMetrics().density);
statusBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, statusBarHeight));
statusBar.setBackgroundColor(act.getResources().getColor(colorResID));
statusBar.setId(13371337);
vg.addView(statusBar, 0);
}
});
return statusBar;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
else if (act.getWindow() != null) {
act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
act.getWindow().setStatusBarColor(act.getResources().getColor(colorResID));
}
return null;
}
private static boolean applyColoredStatusBar( Activity act, int colorResID ) {
final Window window = act.getWindow();
final int flag;
if (window != null) {
View decor = window.getDecorView();
if (decor != null) {
flag = resolveTransparentStatusBarFlag(act);
if (flag != 0) {
decor.setSystemUiVisibility(flag);
return true;
}
else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
act.findViewById(android.R.id.content).setFitsSystemWindows(false);
setTranslucentStatus(window, true);
final SystemBarTintManager tintManager = new SystemBarTintManager(act);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(colorResID);
}
}
}
return false;
}
public static int resolveTransparentStatusBarFlag( Context ctx ) {
String[] libs = ctx.getPackageManager().getSystemSharedLibraryNames();
String reflect = null;
if (libs == null)
return 0;
final String SAMSUNG = "touchwiz";
final String SONY = "com.sonyericsson.navigationbar";
for (String lib : libs) {
if (lib.equals(SAMSUNG)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
}
else if (lib.startsWith(SONY)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
}
}
if (reflect == null)
return 0;
try {
Field field = View.class.getField(reflect);
if (field.getType() == Integer.TYPE) {
return field.getInt(null);
}
} catch (Exception e) {
}
return 0;
}
#TargetApi(Build.VERSION_CODES.KITKAT)
public static void setTranslucentStatus( Window win, boolean on ) {
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
}
else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
I know this doesn't answer the question, but with Material Design (API 21+) we can change the color of the status bar by adding this line in the theme declaration in styles.xml:
<!-- MAIN THEME -->
<style name="AppTheme" parent="#android:style/Theme.Material.Light">
<item name="android:actionBarStyle">#style/actionBarCustomization</item>
<item name="android:spinnerDropDownItemStyle">#style/mySpinnerDropDownItemStyle</item>
<item name="android:spinnerItemStyle">#style/mySpinnerItemStyle</item>
<item name="android:colorButtonNormal">#color/myDarkBlue</item>
<item name="android:statusBarColor">#color/black</item>
</style>
Notice the android:statusBarColor, where we can define the color, otherwise the default is used.
Make Theme.AppCompat style parent
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:colorPrimary">#005555</item>
<item name="android:colorPrimaryDark">#003333</item>
</style>
And put getSupportActionBar().getThemedContext() in onCreate().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getSupportActionBar().getThemedContext();
}
**
Solution for naughty LOLLIPOP
**
hello i had rly big problem with having that darker than was my color... so this is solution to avoid that shadow behind status bar from solution with TranslucentStatus...
so in all of your java activity you need this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
and at your styles.xml you need to set background color (this color will be your status bar color):
<style name="AppCompat" parent="Theme.AppCompat">
<item name="android:colorBackground">#color/YOUR_STATUS_BAR_COLOR</item>
</style>
and at all your layouts you need to add layout which will be your background:
<LinearLayout
android:background="#color/YOUR_BACKGROUND_COLOR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
/>
of course if you would like use #color/just name... you need to set that at colors.xml:
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
here is whole process how we did done that: Whats the right approach to create or change color of a status bar?