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

2
  • 1
    possible duplicate of HTML in string resource? Commented Aug 24, 2015 at 7: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? Commented Aug 24, 2015 at 7:55

1 Answer 1

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

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

3 Comments

I'll see what I can do about leaving the operator as a CharSequence. Edit: How do I replace "y" with "some stuff"?
Should I ask a separate question for that?
I will ask a separate question. I can't seem to find anything.

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.