Vicky
Vicky

Reputation: 170

Apply different size string/text to TextView

enter image description here

I have a text '$170.00' and I want to apply this text to TextView as shown in image. How could be it possible.?? Any suggestion would be appreciated. Thanks in advance.

Upvotes: 2

Views: 1115

Answers (5)

David Corsalini
David Corsalini

Reputation: 8208

1) Create a SpannableString from your original text

SpannableString string = new SpannableString(originalText)

2) Set a SuperscriptSpan and a RelativeSizeSpan for the $ symbol

string.setSpan(new SuperscriptSpan(), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
string.setSpan(new RelativeSizeSpan((0.5f), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

3) Leave the integer part alone.

4) Get the index for the decimal part and apply a SuperscriptSpan and a RelativeSizeSpan to it.

string.setSpan(new SuperscriptSpan(), originalText.lenght - 2, originalText.lenght - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
string.setSpan(new RelativeSizeSpan((0.5f),  originalText.lenght - 2, originalText.lenght - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

5) Apply the SpannableString to the TextView

textView.setText(string);

Upvotes: 5

Hamid Shatu
Hamid Shatu

Reputation: 9700

You can through layout XML as follows...

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/first_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/second_text"
        android:text="$"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/second_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/first_text"
        android:text="170"
        android:includeFontPadding="false"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/third_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/second_text"
        android:layout_toRightOf="@+id/second_text"
        android:text="OO"
        android:textSize="20sp" />

</RelativeLayout> 

Upvotes: 0

InnocentKiller
InnocentKiller

Reputation: 5234

Try it something like below.

((TextView)findViewById(R.id.text)).setText(Html.fromHtml("<sup>$</sup>170<sup>00</sup>"));

with sup the text is going UP and with sub the text is going down.

Upvotes: 3

Ondřej Z
Ondřej Z

Reputation: 5174

You can use SpannableString to format content of TextView:

SpannableString

Or you could use three different TextView in horizontal LinearLayout.

Upvotes: 1

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

it will be done by spannable text what you want to do. but i dont know much about spannable text.

see here spannable on android for textView

may be this may give you some idea.

Upvotes: 0

Related Questions