Aaron
Aaron

Reputation: 4480

How can I make the text in TextView be two different sizes?

I want to write a line in a TextView where the second word is a quarter of the size of the first word. In my string.xml file I used large and small and it did not work to the extent I want it to. I also tried changing the text size in the main.xml, but it only lets you do that once in a TextEdit. Any ideas?

Upvotes: 4

Views: 3643

Answers (3)

Diego Torres Milano
Diego Torres Milano

Reputation: 69396

If what you need is to specify different font sizes for the text in a TextView from XML you can assign a string like this:

<string name="hello"><big><big>Hello Big Text!</big></big>\n<small>Hello small text</small></string>

and use this string in the TextView. Note that the newline is '\n' instead of <br/>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:text="@string/hello" />

Upvotes: 4

inazaruk
inazaruk

Reputation: 74790

Take a look at this question: Is it possible to change the text color in a string to multiple colors in Java?

But instead of ForegroundColorSpan you should use TextAppearanceSpan or AbsoluteSizeSpan.

Upvotes: 1

MrZander
MrZander

Reputation: 3120

Try using the fromHTML method of the HTML class.

This way you can format the string using basic HTML <font> tags

Upvotes: 0

Related Questions