Tal Barda Tal Barda - 5 months ago 16
Android Question

Using bold text in remote view

//Setting our own layout for notification (instead of the regular android notification)
RemoteViews contentView = new RemoteViews(packageName, R.layout.new_event_notification);
contentView.setTextViewText(R.id.eventName, eventToDisplay.getName());

DateTime todayDate = new DateTime().now();


//If the event's date equal to today's date
if ((Days.daysBetween(eventToDisplay.getEventDate().withTimeAtStartOfDay(),todayDate.withTimeAtStartOfDay()).getDays())==0)
{
SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD),0,4,0);
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");

}


I'm trying to make the word "today" bold, and I found something in this forum as you can see... But it didn't work.

EDITED CODE:

//If the event's date equal to today's date
if ((Days.daysBetween(eventToDisplay.getEventDate().withTimeAtStartOfDay(),todayDate.withTimeAtStartOfDay()).getDays())==0)
{
SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);


}

Answer

That is because you are removing the spans via your concatenation. Either use TextUtils.concat() instead of +, or apply your BOLD span after the string concatenation.


In Java programming, concatenation creates new objects.

Replace:

SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);

with:

SpannableStringBuilder eventDayOfTheWeekTxt=new SpannableStringBuilder("Today");

eventDayOfTheWeekTxt.append("(");
eventDayOfTheWeekTxt.append(eventToDisplay.getEventDate().dayOfWeek().getAsText());
eventDayOfTheWeekTxt.append(")");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt);
Comments