Pkmmte Pkmmte - 1 month ago 5
Android Question

Spanned Textview HTML formatting only applies to last in loop?

I need to be able to loop through all my elements and set it to a Textview. However, I also need my numbers to be formatted to subscript. I tried doing so by using a loop but only the last number was formatted correctly.

For example, if I entered Fe2Zn7Ag4, only the "4" will be in subscript and the rest will be like normal.
Here is my code:

Spanned EF = Html.fromHtml("");
for(int g = 0; g < numSaved; g++)
{
EF = Html.fromHtml(EF + savedChem[g].getFormula());
if(!(savedChem[g].getMoles().equals("1")) && !(savedChem[g].getMoles().equals("0")))
EF = Html.fromHtml(EF + "<sub>" + savedChem[g].getMoles() + "</sub>");
}
EmpiricalFormula.setText(findEmpirical());


Also, anything that is subscript gets cut in half. When I run the program, only the top half of the subscript number will appear.

Any idea how to fix both of this issues?

Answer

I found my answer. Turns out Html.fromHtml() converts my previous Spanned object into a String every loop so you only end with the last one. I fixed this with the following code:

Spanned[] E = new Spanned[numSaved];
for (int a = 0; a < numSaved; a++)
{
    EF =(Spanned) TextUtils.concat(EF, E[a]);
}

I also fixed my subscript issue by adding "small" tags.

<sub><small>" + savedChem[g].getMoles() + "</small></sub>
Comments