Change Status Bar Colour in Chrome Custom Tabs - android

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));
}

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);
}
}

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));

Set status bar color dynamically in 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

Setting progress bar color to default prior to API 21

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!

How do I change the color of this bar when switching apps?

With the addition of Lollipop, it appears you can now change the color of this window when changing apps. I don't know what it is called and therefore can't find any info on it but if you look at the image you can see that the Keep app is now yellow.
How do I go about changing this color?
Here is a link to the image, it won't let me attach it since I'm new
Thanks
You can use setTaskDescription() to achieve this:
setTaskDescription(new ActivityManager.TaskDescription(label, icon, color));
For android documentation:
Sets information describing the task with this activity for
presentation inside the Recents System UI. When getRecentTasks(int,
int) is called, the activities of each task are traversed in order
from the topmost activity to the bottommost. The traversal continues
for each property until a suitable value is found. For each task the
taskDescription will be returned in ActivityManager.TaskDescription.
Parameters taskDescription The TaskDescription properties that
describe the task with this activity
https://developer.android.com/reference/android/app/Activity.html#setTaskDescription(android.app.ActivityManager.TaskDescription)
1.normal way
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String title = getString(R.string.app_name);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
int color = getResources().getColor(R.color.color_primary);
setTaskDescription(new ActivityManager.TaskDescription(title, icon, color));
}
2.reflected
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
Class<?> clazz = Class.forName("android.app.ActivityManager$TaskDescription");
Constructor<?> cons = clazz.getConstructor(String.class, Bitmap.class, int.class);
Object taskDescription = cons.newInstance(title, icon, color);
Method method = ((Object) BaseActivity.this).getClass().getMethod("setTaskDescription", clazz);
method.invoke(this, taskDescription);
} catch (Exception e) {
}
}
Simply put this code into the onCreate method of your target activity:
int color = getResources().getColor(R.color.your_top_bar_color);
setTaskDescription(new ActivityManager.TaskDescription(null, null, color));
Please be aware that the code above requires API level 21 (Android 5.0 Lolipop) or higher. In case you need to support older devices as well, you can surround the code with following condition:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int color = getResources().getColor(R.color.your_top_bar_color);
setTaskDescription(new ActivityManager.TaskDescription(null, null, color));
}
Also be aware that you will need to set the colour of top bar in every activity, otherwise your color will be reset when launching another activity. (You can ease this problem by putting the code into some kind of BaseActivity, from which other Activities will inherit.)
Helpful article about this topic: https://www.bignerdranch.com/blog/polishing-your-Android-overview-screen-entry/

Categories

Resources