简体   繁体   中英

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 .

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

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