-->

SubscriptSpan and Superscript span example in Sketchware

SubscriptSpan example

The span that moves the position of the text baseline lower.

Note: Since the span affects the position of the text, if the text is on the last line of a TextView, it may appear cut.

Constructor:
SubscriptSpan()
Creates a SubscriptSpan.

This example shows use of SubscriptSpan in Sketchware.

1. In main.xml add a TextView textview1.

2. In MainActivity.java, create a new String mystring.

3. In onCreate of MainActivity.java:
a. Set the text of mystring to:
H2O = Water

b. Use an add source directly block to put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);

// Add SubscriptSpan to 2 of H2O in above text.
ss.setSpan(new android.text.style.SubscriptSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// Display the Spannable String in TextView.
textview1.setText(ss);

4. Save and run the project. It will show result as in image below.



SuperscriptSpan example

The span that moves the position of the text baseline higher.

Note: Since the span affects the position of the text, if the text is on the first line of a TextView, it may appear cut. This can be avoided by decreasing the text size with an AbsoluteSizeSpan.

Constructor:
SuperscriptSpan()
Creates a SuperscriptSpan.

This example shows use of SuperscriptSpan in Sketchware.

1. In main.xml add a TextView textview1.

2. In MainActivity.java, create a new String mystring.

3. In onCreate of MainActivity.java:
a. Set the text of mystring to:
We three are the 1st, 2nd and 3rd here.

b. Use an add source directly block to put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);

// Add SuperscriptSpan to 'st' of 1st, 'nd' of 2nd and 'rd' of 3rd in above text.
ss.setSpan(new android.text.style.SuperscriptSpan(), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new android.text.style.SuperscriptSpan(), 23, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new android.text.style.SuperscriptSpan(), 31, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// Display the Spannable String in TextView.
textview1.setText(ss);

4. Save and run the project. It will show result as in image below.