-1

In my app, I want to display a string in a text view with html markup. this will get the string:

String mystring = getResources().getString(R.string.Terms_And_Conditions);

except won't allow html characters. I can see on forums to use getText() instead but it doesn't work

mystring = getResources().getText(R.string.Terms_And_Conditions);

I get the error

error: incompatible types: CharSequence cannot be converted to String

In all the examples of using html in a text view, they just had a simple hard coded string, and that works fine. But pulling the string from the resources, it just eats all the html, like the html tags aren't their, but also it didn't actually do the markup. Everyone says I need to use getText() instead. So how can I use getText() to get a string resource or how can I display the html in my string resource? Or do I just have to hard code the string directly in Java?

Thank you in advance.

noone392
  • 920
  • 2
  • 11
  • 24
  • https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview – Mohammed Alaa Jul 06 '20 at 20:35
  • yeah i was on that thread. I just couldn't figure out how to do the function call. – noone392 Jul 06 '20 at 20:41
  • Plain `String`s don't carry any formatting information. Declare your `mystring` as a `CharSequence` instead, and use `getText()` without the `toString()` call. – Mike M. Jul 06 '20 at 21:36

2 Answers2

1

If you want to use getText() you have to proceed like this:

    String mystring = activity.getResources().getText(R.string.Terms_And_Conditions).toString()
1

To allow html characters, you can wrap the resource value in <![CDATA[ ... ]]>

For example, if I want to display the string <button></button>:

strings.xml:

<string name="button_button"><![CDATA[<button></button>]]></string>

MainActivity.kt:

val mystring:String = getResources().getText(R.string.button_button).toString();
Log.wtf("mystring", mystring)

Log output:

E/mystring: <button></button>
Bethany
  • 11
  • 3