I´m writing a calculator for chemistry in Android Studio and I have no idea about how to write the index of the elements in the given form:
H2O
With the index in the bottom left.
How can I achieve this?
I´m writing a calculator for chemistry in Android Studio and I have no idea about how to write the index of the elements in the given form:
H2O
With the index in the bottom left.
How can I achieve this?
 
    
     
    
    You will have to use SubscriptSpan. From official android documentation :
https://developer.android.com/reference/android/text/style/SubscriptSpan
 
    
    You might also want to take a look on this answer Subscript and Superscript a String in Android
Basically, since you need both subscript and superscript, a way to do this is through HTML, and you can add HTML on android.
TextView allows you to insert HTML.
 
    
    The simplest way is to use html- Do like this:-
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("H<sub>2</sub>O"));
 
    
    private static CharSequence makeStringLikeFormula(String str) {
    if (str == null) return "";
    final SpannableString spannable = new SpannableString(str);
    final Matcher matcher = Pattern.compile("\\d+").matcher(str);
    while (matcher.find()) {
        spannable.setSpan(new SubscriptSpan(), matcher.start(), matcher.end(), 0);
    }
    return spannable;
}
example
textView.setText(makeStringLikeFormula("H2O22"));