Can I use SpannableStrings
in a widget's textView? I tried and all it rendered was plain text. I don't know if it is something I'm doing wrong (most likely), or if it just isn't possible for some reason.
Here's the code I'm using (nothing special really...)
public static void updateWidgetState(Context paramContext, String paramString, Integer appWidgetId) {
SpannableString majorLabel = new SpannableString("");
SpannableString minorLabel = new SpannableString("");
if (position > -1) {
majorLabel = GetParsedMajorLabel(paramContext);
minorLabel = GetParsedMinorLabel(paramContext);
}
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString, appWidgetId);
localRemoteViews.setTextViewText(R.id.majorlabel, majorLabel);
localRemoteViews.setTextViewText(R.id.minorlabel, minorLabel);
}
If you would like to display formatted text in a TextView
, use fromHtml
String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>";
textView.setText(Html.fromHtml(link));
you can easily set Spannable String in widget. what I did is I used SpannableStringBuilder
as set it to remoteView's textView
.
views.setTextViewText(R.id.rsTV, WidgetUtils.setPriceSpannableHomeScreenString(currentUserBalance.balance, context))
fun setPriceSpannableHomeScreenString(price: String, context: Context?): SpannableStringBuilder {
val builder = SpannableStringBuilder()
if (AppClass.hasValue(price) && context != null) {
val st = StringTokenizer(price.trim { it <= ' ' }, ".") //pass dot as delimeter
val balanceArrayList = ArrayList<String>()
//iterate through tokens
while (st.hasMoreTokens()) {
balanceArrayList.add(st.nextToken("."))
}
val priceRsText = context.getString(R.string.rs)
var priceBeforeDecimal = ""
var priceAfterDecimal = ""
if (balanceArrayList.size > 0) {
if (balanceArrayList.size > 0 && balanceArrayList.get(0) != null) {
priceBeforeDecimal = balanceArrayList.get(0)
}
if (balanceArrayList.size >= 2 && balanceArrayList.get(1) != null) {
priceAfterDecimal = balanceArrayList.get(1)
}
}
val firstValue = "$priceRsText.$priceBeforeDecimal."
val lastValue = priceAfterDecimal
val txtSpannable = SpannableString(firstValue)
val boldSpan = StyleSpan(Typeface.BOLD)
txtSpannable.setSpan(RelativeSizeSpan(1.5f), 0, firstValue.length, 0) // set size
txtSpannable.setSpan(boldSpan, 0, firstValue.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
builder.append(txtSpannable)
builder.append(lastValue)
// priceSpannableTextView.setText(builder, TextView.BufferType.SPANNABLE)
return builder
}
return builder
}