My application shows some notifications, and depending on user preferences it might use a custom layout in a notification. It works well, but there is a small problem -- text colors. Stock Android and almost all manufacturer skins use black text against a light background for notification text, but Samsung doesn't: their notification pulldown has a dark background and the text in the default notification layout is white.
So this causes a problem: the notifications that don't use any fancy layouts show up fine, but the one that uses a custom layout is hard to read because the text is black instead of the default white. Even the official documentation just sets a #000
color for a TextView
, so I couldn't find any pointers there.
A user was kind enough to take a screenshot of the problem:
So how do I use the default notification text color from the device in my layouts? I'd rather not start dynamically altering the text color based on phone model, since that requires a lot of updating and people with custom ROM's might still get the problem, depending on the skin they're using.
I've found a very simple solution directly changing the name of the attribute provided by Android.
As you can see in this tutorial: http://www.framentos.com/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/
You only need to use a different attribute:
Hope this can help you!
You should use the colors specified in
android.R.color
For example:
android.R.color.primary_text_light
Custom ROM developers and Android skin designers are supposed to update these so your app's colors can be in line with the rest of the system. This includes making sure your text shows up properly throughout the system.
Solution by Malcolm works fine with API>=9. Here's the solution for older API:
The trick is to create the standard notification object and then traverse the default
contentView
created byNotification.setLatestEventInfo(...)
. When you find the right TextView, just get thetv.getTextColors().getDefaultColor()
.Here's the code that extracts the default text color and text size (in scaled density pixels - sp).
Call
extractColors
ie. in onCreate() of your service. Then when you're creating the custom notification, the color and text size you want are innotification_text_color
andnotification_text_size
:I know it is an old question but it could help someone else ; ) I do this in my app and that works perfect in some few lines :
The solution is to use built-in styles. The style you need is called
TextAppearance.StatusBar.EventContent
in Android 2.3 and Android 4.x. In Android 5.x material notifications use several other styles:TextAppearance.Material.Notification
,TextAppearance.Material.Notification.Title
, andTextAppearance.Material.Notification.Line2
. Just set the appropriate text appearance for the text view, and you will get the necessary colors.If you are interested how I have arrived at this solution, here's my trail of breadcrumbs. The code excerpts are taken from Android 2.3.
When you use
Notification
and set the text by using built-in means, the following line creates the layout:The mentioned layout contains the following
View
which is responsible for viewing notification text:So the conclusion is that the needed style is
TextAppearance.StatusBar.EventContent
, which definition looks like this:You should note here that this style doesn't actually reference any of the built-in colors, so the safest way is to apply this style instead of some built-in color.
One more thing: before Android 2.3 (API Level 9), there were neither styles, nor colors, there were only hard-coded values. If you happen to have to support such old versions for some reason, see the answer by Gaks .
Lookin at this instructions: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView If you set up your background color for the LinearLayout container then you can have your colors in notification for text and the background.
If the default colour for the notification text is defined by the launcher application then you cannot retrieve it from the android defaults settings unless the launcher is sharring this information.
However, have you try to remove this line android:textColor="#000" from your layout so that it can get automatically the default color?