0

I have a messaging widget that displays a list of the user's messages, and unread messages should appear as bold.

My code:

    RemoteViews messageRow = new RemoteViews(mContext.getPackageName(), R.layout.row_message);

    messageRow.setTextViewText(R.id.row_user_name, mMessages.get(position).getSender());
    messageRow.setTextViewText(R.id.row_time, mMessages.get(position).getDate());

    if (!mMessages.get(position).isIsRead()){
        // Bold the textviews if the message is unread
    }

I'm looking for something akin to textView.setTypeface(textView.getTypeface(), BOLD); that works on widget textviews. This syntax doesn't seem to exist for RemoteViews.

Thanks!

Alex
  • 5
  • 1
  • 5
  • This looks like something you would need to set on the view before making it a remoteview...use android:typeface in layout XML – dazza5000 Jan 08 '18 at 03:32
  • Is there no way of doing it programmatically? I only want specific textviews to appear bolded so I'd still need to unbold messages that are already read. – Alex Jan 08 '18 at 03:35
  • Could you provide more context of the application? It doesn't look like the API has that, but you could have one layout you inflate for read and one for unread. (One is bold typeface the other not)...it looks like you already have that down though. – dazza5000 Jan 08 '18 at 03:38
  • i don't think we can us TypeFace in widgets so we need to convert text into bitmap and set it has image, here is example https://stackoverflow.com/a/4411060/4394827 – Hemanth S Jan 08 '18 at 04:00
  • dazza5000 Thank you, I wound up using this approach. Solved the problem nicely. If you submit this as an answer, I'll go ahead and mark you as best answer. – Alex Jan 08 '18 at 04:43

2 Answers2

0

Try like this .

1.Use SpannableString in your code .

2.Then use new StyleSpan(android.graphics.Typeface.BOLD) or new StyleSpan(android.graphics.Typeface.NORMAL).

3.Set to your RemoteViews

Example

//  SpannableString init
SpannableString mspInt = null;
// the text you want to change the style 
String text = "yourString";
// SpannableString init
mspInt = new SpannableString(text);
    if (!mMessages.get(position).isIsRead()){
    // Bold the textviews if the message is unread
    //  use setSpan to set BOLD to your text
    mspInt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    messageRow.setTextViewText(R.id.your_id, mspInt);
} else {
    //  use setSpan to set NORMAL to your text
    mspInt.setSpan(new StyleSpan(Typeface.NORMAL), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    messageRow.setTextViewText(R.id.your_id, mspInt);
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

Please try this , this works for me Just add "assistant_bold.ttf" file to your asset folder. and then import android.graphics.Typeface to you class

Typeface xyz;

xyz = Typeface.createFromAsset(context.getAssets(), "assistant_bold.ttf"); messageRow.setTypeface(xyz);

diksha
  • 109
  • 8