Lollipop notification background color

10,491

Solution 1

I can't seem to make my notification use default notification background color: it remains gray where it should be white.

No. At least, not on Lollipop.

The darkish-gray background (#ff424242) will be used (in place of white) if you set Notification.MediaStyle on your Notification.Builder instance:

From BaseStatusBar:

protected void applyColorsAndBackgrounds(StatusBarNotification sbn,
        NotificationData.Entry entry) {

    if (entry.expanded.getId() != com.android.internal.R.id.status_bar_latest_event_content) {
        ....
        ....
    } else {
        // Using platform templates
        final int color = sbn.getNotification().color;
        if (isMediaNotification(entry)) {
            entry.row.setTintColor(color == Notification.COLOR_DEFAULT
                    ? mContext.getResources().getColor(
                            R.color.notification_material_background_media_default_color)
                    : color);
        }
    }
 ....
}

isMediaNotification checks if you are using Notification.MediaStyle. The following takes place if this check comes through:

if (No color was set on Notification.Builder instance using `setColor()`) {
    // R.color.notification_material_background_media_default_color 
    // is used as background
} else {
    // The color you set is used instead.
}

R.color.notification_material_background_media_default_color:

<color name="notification_material_background_media_default_color">#ff424242</color>

Perhaps you can confirm this by taking a screenshot of your notification and reading the argb value of its background. If the issue is introduced by MediaStyle, the color value will be as above. Even simpler approach would be to use setColor(int) on your Builder instance and see if that makes a difference.

This could very well be a Lollipop-specific thing since MediaStyle isn't available on < 21.

Solution 2

<color name="notification_background_light">#fffafafa</color>

matched for me in Android 6 (Marshmallow).

See: Guide lines "Grey 50".

Share:
10,491
velis
Author by

velis

See blog SOreadytohelp

Updated on June 04, 2022

Comments

  • velis
    velis almost 2 years

    I can't seem to make my notification use default notification background color: it remains gray where it should be white. At the same time, notification colors are appropriate in kitkat.

    I have implemented suggestions in this question

    There doesn't seem to be any effect on what I implement. The values-v21 simply acts as if it weren't there. Naturally, my target sdk is 21. I just can't seem to find the reason for this.

    The notification is displayed from service using StartForeground. I have also tried NotificationManager.notify(), but it makes no difference.

    Also, the notification is gray even if I only leave the wrapper RelativeLayout in the xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    

    values-v21\styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="NotificationText" parent="android:TextAppearance.Material.Notification" />
      <style name="NotificationTitle" parent="android:TextAppearance.Material.Notification.Title" />
      <style name="NotificationTime" parent="android:TextAppearance.Material.Notification.Time" />
    </resources>
    

    Though these styles aren't used anywhere else (like in my app theme declaration). They are just defined (in values-v21 and values-v9).

  • velis
    velis about 9 years
    Hm, I'm using NotificationCompat.Builder and it doesn't have any setColor or MediaStyle. Also, the gray I get is light gray as in #e0e0e0. This gray is uniform across three different lollipop ROMs / devices.
  • Vikram
    Vikram about 9 years
    @velis #ffe0e0e0 is used for low-priority notifications: <color name="notification_material_background_low_priority_color">#‌​ffe0e0e0</color>. Try leaving the priority at default.
  • Vikram
    Vikram about 9 years
    @velis You're right about MediaStyle not being available in support library. setColor - Link.
  • velis
    velis about 9 years
    That was it! low-priority is the culprit. I'm using it so that there's no icon in the status bar. Setting it any higer will make the background white, but also show the icon. You get the bounty :)
  • Vikram
    Vikram about 9 years
    @velis Thanks. I thought about the issue - a way to keep normal priority & hide the status bar icon - doesn't seem possible yet.
  • velis
    velis about 9 years
    Yeah, for now I "solved" it by setting the icon to a transparent image. Leaves a nasty space though :(
  • Vikram
    Vikram about 9 years
    @velis :)) I tried that too, but didn't suggest it cos of the empty space it left. Would setSortOrder(String) make a difference? I haven't (and probably don't know how to) used it myself. It might do the trick if it pushes your notification to the end of the stack.
  • velis
    velis about 9 years
    The problem is that this particulat notification is set by service.startForeground(). This automatically places it among the service icons and any "normal" notifications are placed later. So no, it doesn't help.