Thursday, October 12, 2017

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

Leave a Comment

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?

5 Answers

Answers 1

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

Answers 2

Try this example :

* Def :*

public static Bitmap icon ;  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);         mBuilder.setContentText("this text not visible");         mBuilder.setLargeIcon(icon);         mBuilder.setPriority(Notification.PRIORITY_DEFAULT);         mBuilder.setContent(contentNotifySmall); // ORI         //mBuilder.setAutoCancel(false);         mBuilder.setCustomBigContentView(contentNotify); 

Answers 3

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; 

Answers 4

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

Answers 5

for background can you try these attributes...

app:backgroundTint="@color/pay" 

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

android:tint="@color/white" 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment