Set status bar color dynamically in android - android

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

Related

How to set the status bar color in the whole device to green as the default phone app does?

I'm making an app that can place and receive VoIP voice calls. As I want to mimic the default phone behavior, I want to do the same the system does when having a phone call: painting the status bar green while having a call, to highlight the user that he's on a call.
Just to clarify to avoid being marked as duplicated: this question is related to changing the status bar color not just in our Activity but in the whole device, as some phones when in a call.
Is it possible to achieve it?
Use something like this this (for versions > 23)
This is my old method. Some things are deprecated, find a solution and fix it
protected void setStatusBarColor(View view, int color, boolean isLight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(color == 0) color = ContextCompat.getColor(this, R.color.colorPrimaryDark);
if(isLight) {
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
}
//getWindow().setStatusBarColor(Color.WHITE);
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
}
}

Change StatusBar color and show ActionMode at the same time

I change status bar color whenever action mode is shown using this piece of code:
override fun onActionModeStarted(mode: ActionMode?) {
super.onActionModeStarted(mode)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window?.statusBarColor = colorBlue
}
}
The problem is action mode shows up with an animation while window.statusBarColo = changes status bar color immediately and as a result status bar color is changed a little bit sooner than action mode being displayed and it is so unpleasant to see. Do you guys have any solution for this?

Change color and theme of fab button

I used custom style resource and used the following code to change the theme which changes action bar and status bar color but fab button theme doesn't change.
getTheme().applyStyle(switchValue? R.style.AppTheme1:R.style.AppTheme2 , true);
The color of Floating action button is defaulted to colorAccent you can change this from code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fab.setBackgroundTintList(ColorStateList
.valueOf(getResources()
.getColor(colorsArray[themeId],getTheme())));
}
the valueOf method in the above code takes color of type int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fab.setBackgroundTintList(ColorStateList
.valueOf(YourColorValue)));
}

How to set light status bar for Android N

I use this way to set light status bar for Android M, and it works well:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
But, it does not work at Android N. The words in status bar are still white. Is it a bug? Or is there any way to set light status bar at Android N?
Well, I found the question.It did works to set light status bar mode.But after that, i changed the status bar's color, called setSystemUiVisibility() again and set other value.So the light status bar is not valid.
Just call this method from any activity and pass color code too
public static void makeStatusBarLight(Activity activity, int color) {
Window window = activity.getWindow();
window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(color);
}
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
call like this
makeStatusBarLight(BaseActivity.this, getResources().getColor(R.color.yourcolor));

Change Status Bar Colour in Chrome Custom Tabs

I am using Chrome Custom Tabs to display web content in my app. Obviously one of the main advantages of this is the ability to change UI colours in the tab. However, I need to change the status bar colour to something other than a darker version of the primary colour I provide.
Is there anyway to do this?
For reference, here is my code as it stands.
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
builder.setSecondaryToolbarColor(getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
As you can probably guess, I want to change the status bar colour to R.color.colorPrimary rather than the automatically selected colour.
Any help is greatly appreciated
As for now, you cannot change the status bar color when using Custom Tabs. You can check it by yourself from the source code of CustomTabsIntent.Builder to see what you can customize or see the documentation.
I don't try it by myself yet, but if you are targeting api >= 21 (Lollipop), I think the following code could be a work around.
#Override
public void onStart() {
super.onStart();
setStatusBarColor(R.color.colorPrimaryDark);
}
private void setStatusBarColor(int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, colorId));
}
private void showUrl(String url) {
setStatusBarColor(R.color.colorPrimary);
yourCustomTabsIntent.launchUrl(this, Uri.parse(url));
}

Categories

Resources