Dber
Dber

Reputation: 21

Superscripting with SpannableStringBuilder

i'm making a math tool, and i've run into a bit of trouble with the graphical representation of it.

I'm trying to superscript some strings, and the method i'm using is

    tV = (TextView) findViewById(R.id.textView);

    String textEAS;
    String textEBS;
    String textEXS;
    String textEYS;

    textEAS = "A";
    textEBS = "B";
    textEXS = "X";
    textEYS = "Y";

    int lengthA = textEAS.length();
    int lengthB = textEBS.length() + lengthA+3;
    int lengthX = textEXS.length() + lengthB + lengthA;
    int lengthY = textEYS.length();
    String textComb = ( textEBS + " * " + textEAS + textEXS + " = " + textEYS);



    SpannableStringBuilder cs = new SpannableStringBuilder(textComb);
    cs.setSpan(new SuperscriptSpan(), lengthB, lengthX, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    cs.setSpan(new RelativeSizeSpan(0.75f), lengthB, lengthX, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    tV.setText(cs);

It's working fine and putting out the text like B*A^X=Y (with superscripting the x), but the moment i add more than one letter to the string textEXS everything goes down to one line and the superscripting is lost.

Upvotes: 1

Views: 59

Answers (0)

Related Questions