adithya321
adithya321

Reputation: 337

How to change Notification action text color in Android N?

Android N Notification actions

Is it possible to change the text color of 'FIRE'/'AMBULANCE'/'POLICE'?

Or add icons to them like in older versions of Android?

Pre N Android Notification actions

Upvotes: 10

Views: 14984

Answers (5)

Jamil Hasnine Tamim
Jamil Hasnine Tamim

Reputation: 4448

In kotlin you can add this function for text color change:

private fun getActionText(@StringRes stringRes: Int, @ColorRes colorRes: Int): Spannable? {
            val spannable: Spannable = SpannableString(applicationContext.getText(stringRes))
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                spannable.setSpan(
                    ForegroundColorSpan(applicationContext.getColor(colorRes)), 0, spannable.length, 0
                )
            }
            return spannable
        }

After that you just call it from builder:

builder.addAction(R.drawable.ic_call_end, getActionText(R.string.reject,R.color.red), rejectPendingIntent)
builder.addAction(R.drawable.ic_accept, getActionText(R.string.accept,R.color.green), acceptPendingIntent)

You can add more button in your notification.

Upvotes: 2

CatCap
CatCap

Reputation: 240

To add formatting in your text (bold, italic, line breaks, color and so on), you can add styling with HTML markup

Upvotes: 0

Sdghasemi
Sdghasemi

Reputation: 5598

To change action text color use:

HtmlCompat.fromHtml("<font color=\"" + ContextCompat.getColor(context, R.color.custom_color) + "\">" + context.getString(R.string.fire) + "</font>", HtmlCompat.FROM_HTML_MODE_LEGACY)

as the action title CharSequence, like:

Notification notification = new NotificationCompat.Builder(context, channelId)
        ...
        .addAction(new NotificationCompat.Action.Builder(
                            R.drawable.ic_fire,
                            HtmlCompat.fromHtml("<font color=\"" + ContextCompat.getColor(context, R.color.custom_color) + "\">" + context.getString(R.string.fire) + "</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
                            actionPendingIntent))
                    .build())
        .build();

Upvotes: 15

Eric Tjitra
Eric Tjitra

Reputation: 796

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary));

NotificationCompat.Builder.setColor method is used to set an accent color for the notification, which will also be applied to action buttons' text.

Upvotes: 13

ThanosFisherman
ThanosFisherman

Reputation: 5859

To change the text color of actions and also place an icon to the left of them, use following example.

NotificationCompat.Builder(context).setColor(ContextCompat.getColor(context, R.color.yourColorResourceHere))
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Fire", myPendingIntent);

Upvotes: 0

Related Questions