1

Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).

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

After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.

Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?

Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).

Dylanthepiguy
  • 1,621
  • 18
  • 47
  • first of all, xy second, try using it inside program, define it as a String, then iterate through whole string, char by char, and when you get to "y" char, get the position, and afterwards change the char at that position with whatever you want – Vulovic Vukasin Aug 24 '15 at 08:24
  • Oops that > was a typo in the post. The problem is if I define it as a string, the formatting disappears. – Dylanthepiguy Aug 24 '15 at 08:30
  • http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml try this. – Vulovic Vukasin Aug 24 '15 at 08:31
  • I guess that will work. Now is there a better way than just searching for all buttons (containing cdata) and running the Html.fromHtml() on every single one? Edit: Actually hold on. Some of my code reads the string straight from the button titles to figure out what to do. If I turn it into html, retrieving the text will destroy the formatting again? – Dylanthepiguy Aug 24 '15 at 08:37

4 Answers4

0

There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()

Vulovic Vukasin
  • 1,540
  • 2
  • 21
  • 30
0

Let's break down your question in multiple steps:

  • Replacing the y with "other stuff" can be done like this:

    String.format("%1$", "otherStuff");
    
  • If you use getString(), you can do the same thing like that:

    <string name="exponent_key">%1$</string>
    
    ---
    
    String string = getString(R.string.exponent_key, "otherStuff");
    
  • For more than one element, do this way: you can do that like this:

    <string name="string_name">%1$ : %2$</string>
    
    ---
    
    getString(R.string.string_name, new String[]{"hello", "world"});
    
  • In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:

    <string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
    
  • So, with the string above in your XML, just use and you're fine:

    getString(R.string.exponent_key, "otherStuff");
    
  • Note: if you need to show the HTML in a TextView, just use Html.fromHtml:

    textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
    
Filnik
  • 352
  • 1
  • 12
  • 33
  • Whats with the `%2$` ? And shouldn't the CDATA bit be inside the tags like `y]]>`? Could you explain what the format and getString with two arguments does? – Dylanthepiguy Aug 24 '15 at 08:48
  • yeah you're right about the CDATA (fixed in my post), regarding the %2$ etc, I've added it in the post – Filnik Aug 24 '15 at 08:58
  • But what does that %1$ even mean? – Dylanthepiguy Aug 24 '15 at 09:02
  • it's a placeholder.. so the Java knows that the first elements goes in that position. It's like calling: "Hi %1$".replace("%1$", "Master"); It doesn't mean something in particular. The number is incremental, so you know which element replace with which placeholder. – Filnik Aug 24 '15 at 09:08
  • Ok, so I never use the letter "y" in the string resource? I use the string resource as a button title... – Dylanthepiguy Aug 24 '15 at 09:13
  • that's right, you don't need the y. You can use the replace function if you want to use it, but it's useless and the way I showed you it's more standard (and clean) – Filnik Aug 24 '15 at 09:16
  • Just so we are on the same page here, this is my project (if you have time) https://dl.dropboxusercontent.com/u/62863300/Random/ScientificCalculator.zip The string resources is used on those buttons on the main screen and then used in the singleOperandFunctionButtonPressed and calculateAndShowAnswerOnScreen methods. – Dylanthepiguy Aug 24 '15 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87757/discussion-between-filnik-and-dylanthepiguy). – Filnik Aug 24 '15 at 09:31
0

in your string.xml

<string name="exponent_key">x<sup><small>%1$d</small></sup></string>

in your code

textView.setText(getString(R.string.exponent_key,2))
sanemars
  • 767
  • 5
  • 18
0

Consider the effect you want to achieve. you can do like this.

 SpannableString ss = new SpannableString("2");
        // set superscript
        ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // set font size
        ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.append(ss);
sanemars
  • 767
  • 5
  • 18