10

I'm developing an android project and I want to render some chemistry formula.

I wrote the following code and I got the following result.

I create a custom string and show it in a textview.

But my question is this: Is this the best way to do this? And is there another way to handle that?

str = new SpannableString(Html.fromHtml("2H<sup>+</sup> + So<sub size = 2>4</sub><sup size = 2>2-</sup> --> H<sub size =2>2</sub>So<sub size = 2>4</sub>"));
ss1.setSpan(new RelativeSizeSpan(0.6f), 2,3, 0); // set size
ss1.setSpan(new RelativeSizeSpan(0.6f), 8,11, 0); // set size
ss1.setSpan(new RelativeSizeSpan(0.6f), 17,18, 0); // set size
ss1.setSpan(new RelativeSizeSpan(0.6f), 20,21, 0); // set size
TempF.setText(ss1,TextView.BufferType.SPANNABLE);

Example Chemical Formula

2
  • 1
    You found a good solution, that's easy enough to implement for many chemical formulas. On Android, you could have used special custom fonts on a Canvas, LaTeX, vector graphics (through a library), and many other ways to achieve the same result, but I like your solution the best (assuming you don't have any other special requirements that I haven't taken into account). Jun 8, 2014 at 4:19
  • Thanks! Great answer :) SpannableString spannableString = new SpannableString( Html.fromHtml("C<sub>10</sub>H<sub size = 2>14</sub>N<sub size =2>2</sub>")); spannableString.setSpan(new RelativeSizeSpan(0.6f), 1, 3, 0); spannableString.setSpan(new RelativeSizeSpan(0.6f), 4, 6, 0); spannableString.setSpan(new RelativeSizeSpan(0.6f), 7, 8, 0); Jun 10, 2016 at 7:19

3 Answers 3

4
+50

I don't think there is a better way.

Unfortunately Html.fromHtml() ignores <font size="n"> tags, so these spans need to be added manually, as you have done.

2

I had a similar problem to solve. I created a simple library by extending WebView and using JavaScript in the background. https://github.com/RanaRanvijaySingh/EquationView

String strChem = "Chemistry: \\(\\ce{CO2 + C -> 2 CO}\\)";
EquationView equationViewChem = findViewById(R.id.equationViewChem);
equationViewChem.setText(strChem);

In your layout file:

<com.rana.equationview.EquationView
 android:id="@+id/equationViewChem"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>

enter image description here

Hope this helps someone.

0

I think you have found a solution that fits you. Perhaps, if there is a rule to know when a symbol will be a subscript or superscript, or the spansize you have to use, you can write a method to parse a given a formula privided as a string and automatically provide the formated html output.

To do this, and assuming there are specific rules, you can use a finite state machine that will "compile" a text string to the html output you need.

If you are looking for something like this let me know and we may be able to work it out.

Another option would be to see if you find (in Internet) a font that meets your needs (Or perhaps build your own). You can import a font putting the font file in the assets folder and setting it from there in the source.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.