Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chemistry formula on Android

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

like image 262
strings95 Avatar asked May 02 '14 12:05

strings95


People also ask

How do you write chemical formulas on a phone keyboard?

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.

Is there an app that solves chemistry?

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!

How do you find the formula in chemistry?

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).


2 Answers

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.

like image 91
Rana Ranvijay Singh Avatar answered Sep 28 '22 23:09

Rana Ranvijay Singh


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.

like image 24
matiash Avatar answered Sep 29 '22 01:09

matiash