2

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.

Vicky
  • 170
  • 12

5 Answers5

5

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);
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
3

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.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • Nop it looks like SuperScript for 170. – Vicky Mar 27 '14 at 09:32
  • @Vicky, obviously it will look like that only, what else you want then. – InnocentKiller Mar 27 '14 at 09:33
  • They should look on same lavel. – Vicky Mar 27 '14 at 09:34
  • Hi I know it can be done using HTML Style. and I have written code for it but don't know how to use it. If u know then can u tell me how to use it? I will post the html code. – Vicky Mar 27 '14 at 09:37
  • /* Untitled Document

    $ 170 00

    */
    – Vicky Mar 27 '14 at 09:38
1

You can use SpannableString to format content of TextView:

SpannableString

Or you could use three different TextView in horizontal LinearLayout.

Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
0

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.

Community
  • 1
  • 1
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

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> 
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41