working with Strings - how can I make "small numbers" in the upper part like in real books - is it possible?

StackOverflow https://stackoverflow.com/questions/21621067

  •  08-10-2022
  •  | 
  •  

Question

Sorry , my English is not enough to explain clearly what I want, maybe that's why I can't find answer in the Google.

When you use real book - sometimes you have some text there that needs an explanation and after that text you have "*" sign and an explanation at the bottom of the page under a line.

And sometimes it's not a "*", it's a small number at the top of last word. Like "1" , "2" ,etc, and then same explanation at the bottom of the page under a line.

What I need to know - is there any instrument in java/Android SDK to have this numbers to be small and at the top in TextView?

Like, maybe Spanned Strings can do that?

Was it helpful?

Solution

The easiest way to produce such a Spanned is with the Html utility class:

textView.setText(Html.fromHtml("book<sup>1</sup>"));

OTHER TIPS

You can use SubscriptSpan or SuperscriptSpan within a SpannableString.

Tested Demo

public class MainActivity extends Activity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        textView.setText(Html.fromHtml("Hi this demo test for book reading effect <sup>2</sup>"));
    }

}

OutPut:

enter image description here

You can Achive this 2 Way.

1) Using SuperscriptSpan:

SpannableString span = new SpannableString("book1");
        span.setSpan(new SuperscriptSpan(), 4, 5,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(span, BufferType.SPANNABLE);

2) Using Html:

txt.setText(Html.fromHtml("book<sup>1</sup>"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top