• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm adding some Lollipop-only styling to an app.

I want to change color of header in overview screeen like Gmail here:
enter image description here

I have figured out I can do

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="android:colorPrimary">@color/my_favorite_color</item>

</style>

to achieve it, but I would want to specify only the color for this case, just like I can do:

<item name="android:statusBarColor">@color/my_favorite_color<item>

Is there a specific attribute to set header's color?

Side question: can icon and title be also changed?

    • @alanv Neat, thanks Alan. I would argue about this being a duplicate, because the question you pointed out asks about changing title color, which seems not to be possible. Anyway the answer there perfectly answers my question. If you could answer, I will remove/transfer the bounty, allowing you to vote on closing this question.
      • 2
    • @MaciejGórski Just an improvement(in my opinion): if you already know what color you need to supply, you can override setTaskDescription(TaskDescription) and pass relevant TaskDescription instance to the super call. To trace back from the source, look at TaskViewHeader. You should end at Activity#onApplyThemeResource(....) - this is where setTaskDescription(TaskDescription) is called from.
    • @Vikram Thanks for pointing out onApplyThemeResource. From the code there we can clearly deduct background cannot be changed via app theme just like statusBarColor except for using colorPrimary. I'm not sure relying on setTaskDescription being called by the framework would be a good thing. Not nearly as bad as trying to call ActivityManagerNative.getDefault().setTaskDescription via reflection (which I just did to try to make a different size of icon, but it didn't work), but I think I prefer a more straighforward way of having compat helper class with if (SDK_INT >= LOLLIPOP).
      • 2
    • @Vikram Yes, it's called exactly once before onCreate, but this behavior is not part of the lifecycle, is not documented and developers first looking at the code that does that won't be able to understand it without having comments in the overridden method body explaining how it works or digging deep into Android code. Calling it yourself in onCreate is a straightforward way to state the intent of the code and even if a developer didn't know this method existed, they could just look at javadoc. I like writing less code, but more than that I like being clear with the code I write.

You can change this via ActivityManager.TaskDescription:

https://developer.android.com/reference/android/app/ActivityManager.TaskDescription.html

From an Activity context, call:

TaskDescription taskDescription = new TaskDescription(label, icon, colorPrimary);
((Activity)this).setTaskDescription(taskDescription);
  • 50
Reply Report
      • 2
    • To clarify more: * Label - text on the card header * Icon - left bitmap icon (usually an icon which looks good on the 'colorPrimary' color * colorPrimary - the card header color (usually same as the Toolbar color, but not necessarily) This solution worked for me, +1 :)
    • Works nicely. Depending on minSdkVersion, may need to be wrapped in if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ... }
    • Works nicely, once you figure out it won't work in onCreate(). Place this code in onResume() and you're good.