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);
There are some steps to be followed to write a chemical equation in MS Word and the steps vary for android and computer. "CH4 + O2 -> CO2 + H2O". Type CH4 and click "Shift + left arrow" it selects "4" and press "Ctrl ++", it subscript(Print below the line)the letter you select. Similarly type O2, CO2, and H2O.
The Chemistry lets you find chemical reactions and to solve the chemical equations with one or multiple unknown variables. You'll always have Mendeleev's Periodic Table and Solubility table handy. And even the calculator of molar masses!
We can obtain the chemical formula from the empirical formula if we know the molecular weight of the compound. The chemical formula will always be some integer multiple of the empirical formula (i.e. integer multiples of the subscripts of the empirical formula).
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"/>
Hope this helps someone.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With