Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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)

Solution 2:

1.normal way

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Stringtitle= getString(R.string.app_name);
        Bitmapicon= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        intcolor= getResources().getColor(R.color.color_primary);
        setTaskDescription(newActivityManager.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) {

            }
}

Solution 3:

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/

Post a Comment for "How Do I Change The Color Of This Bar When Switching Apps?"