Subscript and Superscript a String in Android
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
or
Common Tasks and How to Do Them in Android
How to type x^2 (subscript and superscript) in TextView
Android has native support for sub/superscript type text know as SubscriptSpan and SuperscriptSpan
Sample Usage of Superscript (eg X^2)
String text = "X2";
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.setSpan(
superscriptSpan,
text.indexOf("2"),
text.indexOf("2") + String.valueOf("2").length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(builder);
I have found a really good example.
Superscript and Subscript in Android App XML
I found out how
In the XML file the code should be like this :
<value column="Back" null="false">H<sub>2</sub></value>
So that the parced string value is "H<sub>2</sub>"
then in the java code :
TextView textview.setText(Html.fromHtml(YourString);
and for superscript use "sup" instead of "sub"
Subscript 5 in Android strings.xml
Subscript 1 to 4 are inbuilt. They will always work.
But to use subscript 5 to 9 you need to use html code inside your string
<sub><small>5</small></sub>
to get 5
Same applies for superscript
<sup><small>5</small></sup>
to get this 5
How to create vertically aligned superscript and subscript in TextView
You might want to try something like this.
It's basicall a ReplacementSpan
that takes the text it's applied on, separates it into two parts, and draws them on the canvas. The size factor and y translation are somewhat hand-picked. I hope it's useful (or at least that you or someone else can build on it).
public class SuperSubSpan extends ReplacementSpan
{
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm)
{
text = text.subSequence(start, end);
String[] parts = text.toString().split(",");
Paint p = getSuperSubPaint(paint);
return (int) Math.max(p.measureText(parts[0]), p.measureText(parts[1]));
}
private static TextPaint getSuperSubPaint(Paint src)
{
TextPaint paint = new TextPaint(src);
paint.setTextSize(src.getTextSize() / 2.5f);
return paint;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
{
text = text.subSequence(start, end);
String[] parts = text.toString().split(",");
Paint p = getSuperSubPaint(paint);
float width1 = p.measureText(parts[0]);
float width2 = p.measureText(parts[1]);
float maxWidth = Math.max(width1, width2);
canvas.drawText(parts[0], x + (maxWidth - width1), y - (bottom - top) / 3f, p);
canvas.drawText(parts[1], x + (maxWidth - width2), y + (bottom - top) / 10f, p);
}
}
Then use it as:
Spannable str = new SpannableString("9,4Be -> 2000,127Jo");
str.setSpan(new SuperSubSpan(), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new SuperSubSpan(), 9, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(str);
which produces the following result:
A slightly better solution would be to have a special format for these strings (e.g. "{9,4}Be -> {2000,127}Jo"
) and have a regular expression process the string and add the corresponding SuperSubSpans
, just so that you don't need to this manually. But the actual Span part would be more or less the same.
Android Formatting Superscript and Subscript
For your example to format this ^2S_{1/2}, you will write your string value as
"<sup><small>2</small></sup> S <sub><small>½</small></sub>".
And write symbols in their codes and also use Html.fromHtml() in your TextView setText() method.
Related Topics
The Import Org.Junit Cannot Be Resolved
Regular Expression to Validate Username
How to Loop Through List of Webelements and Select One Webelement With a Condition
Spring Boot Application Shutdown Immediate After Starting
How to Set the Radio Button Based on the Value Fetched from the Database
Batch Inserts Using JPA Entitymanager
Avoid Keycloak Default Login Page and Use Project Login Page
How to Mock System.Getproperty Using Mockito
How to Inject a Map Using the @Value Spring Annotation
How to Create Comma Separated String in Single Quotes from Arraylist of String in Java
Notifydatasetchanged() Makes the List Refresh and Scroll Jumps Back to the Top
Checking If a String Contains a Dot
Error: Could Not Find or Load Main Class in Intellij Ide
Several Ports (8005, 8080, 8009) Required by Tomcat Server At Localhost Are Already in Use
How to Get Amount of Tens in a Int
Type Safety: Unchecked Cast from Object to Arraylist<Myvariable>
How to Update a Jsf Component from a Jsf Backing Bean Method