Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight left hand side text of every line in notification

Tags:

android

Currently, I had implemented multi-line notification

enter image description here

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this.getApplicationContext())
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(contentTitle)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setTicker(ticker)
            .setContentText(contentText);

    // Need BIG view?
    if (size > 1) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle(contentTitle);
        for (String notificationMessage : notificationMessages) {
            inboxStyle.addLine(notificationMessage);
        }
        mBuilder.setStyle(inboxStyle);
    }

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

However, this is not exactly what I want. When I look at GMail implementation, I realize their left hand side text (Yan Cheng Cheok) of every line, is being highlighted.

enter image description here

I was wondering, what is the technique to achieve such effect? As I would like to highlight my stock names.

like image 727
Cheok Yan Cheng Avatar asked Mar 19 '23 11:03

Cheok Yan Cheng


2 Answers

the code inboxStyle.addLine(notificationMessage); accepts a CharSequence as the parameter, not a String

so you can easily achieve the effect you want by applying UnderlineSpan

follows reference links:

https://developer.android.com/reference/android/text/style/UnderlineSpan.html

How to set underline text on textview?

quick edit:

I just realise the OP mention highlight and not underline but the answer is still the same,there're several types of CharacterSpan the OP can choose to customise the text:

https://developer.android.com/reference/android/text/style/CharacterStyle.html

like image 171
Budius Avatar answered Mar 21 '23 23:03

Budius


You have a few options to make some text bold.

The first option is to use Html.fromHtml(String) and put a bold tag in your text like so:

for (String notificationMessage : notificationMessages) {
    CharSequence content = Html.fromHTML("<b>This is bold</b> and this is not");
    inboxStyle.addLine(content);
}

If you don't want to use HTML, you can also use a Span to accomplish the same thing:

Spannable sb = new SpannableString("Bold text");
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length() - 1, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);
like image 33
Bryan Herbst Avatar answered Mar 22 '23 00:03

Bryan Herbst