L_Xian
L_Xian

Reputation: 1

How to get the notification background color after android N

I want to define the Notification UI,because in some phones the notification background color is white, and in other phones is dark. I want to check what is the background color so to choose a different layout.

Before android N, my code was like this class:

https://github.com/lizixian18/MusicLibrary/blob/master/musiclibrary/src/main/java/com/lzx/musiclibrary/notification/NotificationColorUtils.java#isDarkNotificationBar

The principle is to check the default contentView title and content TextView color. But after android N the default contentView is null.

Notification#build:

public Notification build() {

    if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
            && (useExistingRemoteView())) {
        if (mN.contentView == null) {
            mN.contentView = createContentView();
            mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
                    mN.contentView.getSequenceNumber());
        }
        if (mN.bigContentView == null) {
            mN.bigContentView = createBigContentView();
            if (mN.bigContentView != null) {
                mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
                        mN.bigContentView.getSequenceNumber());
            }
        }
        if (mN.headsUpContentView == null) {
            mN.headsUpContentView = createHeadsUpContentView();
            if (mN.headsUpContentView != null) {
                mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
                        mN.headsUpContentView.getSequenceNumber());
            }
        }
    }

    if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
        mN.flags |= FLAG_SHOW_LIGHTS;
    }
    return mN;
}

So my question is: How to check whether the notification background color is dark or white after Android N?

Upvotes: 0

Views: 916

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200080

It is not recommended to set custom colors on the text of your notification. Instead, you should use the TextAppearance.Compat.Notification styles for your text:

  • TextAppearance.Compat.Notification for regular text in your notification
  • TextAppearance.Compat.Notification.Title for the title style
  • TextAppearance.Compat.Notification.Info (or Line2) for secondary text

These styles automatically have the correct colors already built in.

For media apps, as such is the one you mention, Notifications should be built with NotificationCompat.MediaStyle, which also takes into account the correct background and text colors.

Upvotes: 0

Related Questions