简体   繁体   中英

Android 5+ custom notification XML layout with RemoteViews, set correct icon tint for ImageButton

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, ie 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?

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.

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!!

for background can you try these attributes...

app:backgroundTint="@color/pay"

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

android:tint="@color/white"

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)

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;

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);

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

Hope it's help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM