jamesxbrown
jamesxbrown

Reputation: 135

Change color of notification title (Android)

That's my code and everything works fine.

val builder = NotificationCompat.Builder(this, "NOTI_ID")
        .setSmallIcon(R.drawable.ic_stat_name)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))

The only problem is I can't change the color of the title (= "Hello World"). The last line .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)) changes only the icon-color. I found nothing in the documentation and if you look at those images, you see the title have the same color as the icon. So I'm not sure if the error comes from me or my code missing something?

EDIT

With title I mean the app name. Sorry, it was my mistake. Not the contentTitle, but the name of the app. Check for example those images.

Upvotes: 2

Views: 3970

Answers (1)

Sdghasemi
Sdghasemi

Reputation: 5598

I'm using this approach in Java:

int color = ContextCompat.getColor(this, R.color.colorPrimaryDark);
builder.setContentTitle(HtmlCompat.fromHtml("<font color=\"" + color + "\">" + notificationTitle + "</font>", HtmlCompat.FROM_HTML_MODE_LEGACY))

Upvotes: 5

Related Questions