Use SpannableString in Android Home Screen widgets?
Asked Answered
H

5

1

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);
 }
Hokum answered 4/6, 2012 at 20:4 Comment(4)
Can you show your code where you get the Spanned object and how you put it into the RemoteViews?Understate
code added, but I don't think it will help much.Hokum
Have you tried a simpler scenario, such as using Html.fromHtml() to create the Spanned object out of some HTML snippet, in case the problem lies in your GetParsed...Label() methods?Understate
I use that same code to display the text in other places in my app (non-widget), and it seems to be working fine there. But I will give fromHtml a try.Hokum
G
-1

If you would like to display formatted text in a TextView, use fromHtml

Grandmotherly answered 4/6, 2012 at 20:15 Comment(2)
fromHtml just returns a Spannable object, so I don't think it will be any different than using SpannableString. But the fact that you are suggesting it makes me think that it indeed can be possible so I must be goobering it up somewhere.Hokum
This does not work in a widgetPleo
P
0

You can use setInt to set a widget TextView to a string with HTML markup

views.setInt(R.id.textview_id, "setText", R.string.widget_text)

and in strings.xml

<string name="widget_text">This is an <u>example</u>.</string>

I'm not aware of setting raw SpannableStrings being possible, definitely not if they contain custom spans. But maybe there's a method you can call with views.setCharSequence to set Html without a string resource

Pleo answered 17/7, 2024 at 16:47 Comment(0)
G
-1

If you would like to display formatted text in a TextView, use fromHtml

Grandmotherly answered 4/6, 2012 at 20:15 Comment(2)
fromHtml just returns a Spannable object, so I don't think it will be any different than using SpannableString. But the fact that you are suggesting it makes me think that it indeed can be possible so I must be goobering it up somewhere.Hokum
This does not work in a widgetPleo
W
-1
String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>"; 
textView.setText(Html.fromHtml(link));
Workingwoman answered 4/6, 2012 at 20:23 Comment(1)
This does not work in a widgetPleo
S
-1

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
}
Scrip answered 8/11, 2018 at 7:53 Comment(1)
This answer doesn't answer the questionPleo
F
-1

Update 2020 Html.fromHtml is deprecated in Android N

Use HtmlCompat.fromHtml for better compatibility

Example setting Bold text with HTML

 setTextViewText(
                  R.id.wind, HtmlCompat.fromHtml(
                            "<b>Wind:</b>"+ windSpeedKmh+"<br/>"+
                            "<b>Direction:</b>" +windDirection
                       , HtmlCompat.FROM_HTML_MODE_LEGACY)
                    )
Footwork answered 15/6, 2020 at 10:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.