3

Quick question with android InboxStyle(), can I have a colored string? I tried using Spannable like

NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
text = "sample";
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), 0);
style.addLine(spannable);

but no luck... :(

Am I able to add colors? Thanks!

diggin
  • 47
  • 6

1 Answers1

6

Change you flag in setSpan on Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

 spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

UPDATE

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher);
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle("title");
    inboxStyle.setSummaryText("summarytext");
    String lineFormatted = "test";
    Spannable sb = new SpannableString(lineFormatted);
    sb.setSpan(new ForegroundColorSpan(Color.RED), 0, lineFormatted.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inboxStyle.addLine(sb);
    builder.setStyle(inboxStyle);
    builder.setNumber(1);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, builder.build());
Dima
  • 1,189
  • 1
  • 13
  • 31