20

My app is using a custom Notification layout with RemoteViews.

To display text, the layout is using the following system styles:

android:TextAppearance.Material.Notification.Title android:TextAppearance.Material.Notification

This works fine.

However, the TextAppearance style can't be used to set the value of android:tint, so I had to hardcode the color.

To my best knowledge, there's no special system style for setting notification ImageButton tint.

Hardcoded colors work fine on the current Android 5+ systems, but some users install custom ROMs with custom dark themes, and the notification looks wrong, i.e. black icons on black background.

Is there any way to get the system notification icon / imagebutton color, and apply it from an XML layout?

Or maybe there's another way to achieve this?

Alexey Yakovenko
  • 494
  • 2
  • 10
  • 1
    Do you want to update the ImageButton which is in the `custom_notification_layout` or do you want to update the Notification icon which appears on the top notification bar ? – Nilesh Deokar Oct 05 '17 at 15:52
  • 1
    pls add your custom notification layout – JAAD Oct 06 '17 at 14:11
  • 1
    may be this will get you an idea to achieve what you've asked: https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color – Sudheesh R Oct 09 '17 at 10:59
  • 3
    @NileshDeokar the question is about the custom notification layout, yes. I didn't come up with any good solution to that problem still, and I got some new problems related to that one later on. I didn't get to fixing that either, but I'm planning to go with designing 2-3 different custom layouts with hardcoded colors, then analyze current colorscheme, then use the layouts with different colors based on that. – Alexey Yakovenko Oct 10 '17 at 11:52
  • @AlexeyYakovenko have you managed to make your notifications working? In my app I use the system style `android:TextAppearance.Material.Notification.Title` and `android:TextAppearance.Material.Notification.Time`, but it makes black text on black background on Huawei Y6 II. So even using system styles doesn't guaranties that your notifications will look correctly. What a shame... – Mikalai Daronin Apr 04 '18 at 14:25

6 Answers6

2

Try this example :

Definition for notification :

// Declare variable
public static Bitmap icon;

// Assign value but this line can be different depends on current 
// android sdk vesion ...
icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.YOUR_IMAGE);

     mBuilder = new NotificationCompat.Builder(this);
     mBuilder.setShowWhen(false);
     mBuilder.setDefaults(Notification.DEFAULT_ALL);
     mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
     mBuilder.setSmallIcon(R.drawable.image1); // One way to load img
     mBuilder.setContentText("this text not visible");
     mBuilder.setLargeIcon(icon);// This is the line
     mBuilder.setPriority(Notification.PRIORITY_DEFAULT);
     mBuilder.setContent(contentNotifySmall); // ORI
     //mBuilder.setAutoCancel(false);
     mBuilder.setCustomBigContentView(contentNotify);

I setup small and big variant for any case , this is important.

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
2

Sorry, But as per my knowledge custom ROM's have separate system designs,configurations and that are not official as well.

So,supporting Custom ROM without knowledge about its design is not possible. And android APIs are for supporting official ROM's.

Hope it Helps!!

Animesh Mangla
  • 773
  • 7
  • 31
1

for background can you try these attributes...

app:backgroundTint="@color/pay"

---------Or-------------

android:tint="@color/white"
kiturk3
  • 549
  • 10
  • 30
1

You can take textColor from TextAppearance_Compat_Notification_Title and add it as tint to an image programmatically like this

val remoteViews = RemoteViews(service.packageName, R.layout.notification_layout)

val icon = Icon.createWithResource(context, R.drawable.icon)
val attrs = intArrayOf(android.R.attr.textColor)
with(context.obtainStyledAttributes(R.style.TextAppearance_Compat_Notification_Title, attrs)) {
    val iconColor = getColor(0, Color.GRAY)
    icon.setTint(iconColor)
    recycle()
}
remoteViews.setImageViewIcon(R.id.ibPlayPause, icon)
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

You can use a notificationlistenerservice to get an active notification. This returns a list of StatusBarNotifications, then just:

StatusBarNotifcation sBNotification = activeNotifications[0];
Notification notification = sBNotification.getNotification();
int argbColor = notification.color;
Adrian Le Roy Devezin
  • 672
  • 1
  • 13
  • 41
0

If you change ImageButton to ImageView in your layout you can update it with

    RemoteViews remoteView = getRemoteViews(context); 
     // load base icon from res
    Bitmap baseIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.base_icon);
     // edit Bitmap baseIcon any way for your choose
    Bitmap editedBitmap = ...
    // update notification view with edited Bitmap
    remoteView.setImageViewBitmap(R.id.button_icon, edited);

P.S. you can edit Bitmap like this: https://stackoverflow.com/a/5935686/7630175 or any other way

Hope it's help

Nazar P.
  • 57
  • 4