views:

43

answers:

1

I can't manage to get my textSwitcher to be able to support superscript text. No matter what I try it shows up as regular size text.

Here is what I've tried:

        Spanned span = Html.fromHtml("<sup>TM</sup>");
        String subscript = span.toString();
        mSwitcher.setText(getText(R.string.desc_about1) + subscript);

Then I tried setting it in the XML string file:

<string name="desc_about1">Android<sup><small>TM</small></sup></string>

Neither one of these will work. Is there another way?

My textSwitcher code for reference:

    <TextSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


@Override
public View makeView() {
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.LEFT);
    t.setTextSize(18);
    return t;
}

Thanks all!

+2  A: 

Your first example looks incorrect for me, try this instead:

    Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>");
    mSwitcher.setText(span);

Full example that works for me:

package com.stackoverflow;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class Test extends Activity implements ViewFactory {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextSwitcher ts = (TextSwitcher) findViewById(R.id.switcher);
        ts.setFactory(this);
        ts.setText(Html.fromHtml("Test<sup>TM</sup>"));
    }

    @Override
    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.LEFT);
        t.setTextSize(18);
        return t;
    }

}

Konstantin Burov
Unfortunately, that did not work either.
Strange.. I've just tried it and it works for me. Are you sure you're passing Html.fromHtml() output directly to setText method? In your case you had converted it into String and that was the problem.
Konstantin Burov
Updated answer with more full example..
Konstantin Burov