1

I have many strings like this:

<string name="exponent_key">x<sup><small>y</small></sup></string>

And when I set the text of a TextView, in an xml layout file, the result is a correctly superscripted 'y'.

However if i use getString() (or event try to retrieve the text, from the TextView mentioned above, using .getText()) and then use setText() to apply the changes, the 'y' gets changed back into normal text (so the result is just 'xy').

Is it possible to work around this? I was hoping that the html code is retained in the string so that i can use a .replace() to swap x and y with actual numbers later on, so that it would look like the real maths format. There are many operator strings like this so it will be difficult to manually re-add the html for each one.

Just in case you want to see my program: https://dl.dropboxusercontent.com/u/62863300/Random/ScientificCalculator.zip

Dylanthepiguy
  • 1,621
  • 18
  • 47
  • 1
    possible duplicate of [HTML in string resource?](http://stackoverflow.com/questions/2667319/html-in-string-resource) – Sebastian Aug 24 '15 at 07:52
  • Ah yes! I passed that post off after reading the first answer. So basically I don't convert the text to string ever? Leave as char sequence? – Dylanthepiguy Aug 24 '15 at 07:55

1 Answers1

3

However if i use getString() (or event try to retrieve the text, from the TextView mentioned above, using .getText()) and then use setText() to apply the changes, the 'y' gets changed back into normal text (so the result is just 'xy').

if you are using getResources().getString(), then you have to pass the result of Html.fromHtml to setText(). TextView.getText() returns a CharSequence which is superclass for String and Spannable (among the others). If you assign the return value of getString to a CharSequence, without calling toString(), and then pass it back to setText it will maintain its style

Blackbelt
  • 156,034
  • 29
  • 297
  • 305