2

Is there a way to use variables in this situation? I want this code:

String str = "test";
Html.fromHtml("test <.bold> str <./bold> test");

to make:

test test test

but instead it makes:

test str test



Thanks

2
  • You included "str" inside the quotes. It is a variable and should be added outside quotes as teepee has answered below. Commented Aug 8, 2011 at 20:45
  • <.bold> is not valid HTML and is not honored by Html.fromHtml(). Commented May 2, 2014 at 17:09

2 Answers 2

7

What about this?

String str = "test";
textView.setText( Html.fromHtml("test <bold>" + str + "</bold> test") );

or for a more difficult solution, you can use java's replaceAll for replacing strings defined with regular expression.

Sign up to request clarification or add additional context in comments.

Comments

0

kotlin version using an extension function with Android SDK deprecation validation

@Suppress("deprecation")
fun String.fromHtml(): Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
    Html.fromHtml(this)
}

val str = "test"
textView.text = "test <bold>$str</bold> test".fromHtml()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.