Custom notification layouts and text colors

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, and TextAppearance.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.

  1. When you use Notification and set the text by using built-in means, the following line creates the layout:

    RemoteViews contentView = new RemoteViews(context.getPackageName(),
            com.android.internal.R.layout.status_bar_latest_event_content);
    
  2. The mentioned layout contains the following View which is responsible for viewing notification text:

    <TextView android:id="@+id/text"
        android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:paddingLeft="4dp"
        />
    
  3. So the conclusion is that the needed style is TextAppearance.StatusBar.EventContent, which definition looks like this:

    <style name="TextAppearance.StatusBar.EventContent">
        <item name="android:textColor">#ff6b6b6b</item>
    </style>
    

    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 .

Leave a Comment

tech