5

I've seen some Android apps display decimal numbers where the decimal part has a smaller font, even though they appear to be in a single TextView, like this:

enter image description here

I was wondering how to make something like that.

0ne_Up
  • 471
  • 3
  • 9
  • 24
  • Like this: http://stackoverflow.com/questions/4897349/android-coloring-part-of-a-string-using-textview-settext PS: you can change the color or anything you want there. You should google it a bit more. – Mariano Zorrilla Nov 24 '15 at 12:19
  • Did you get the answer? – Amy Nov 24 '15 at 12:34

2 Answers2

7

Try to set HTML string to TextView

String text = "831<sup>69</sup>";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
Amy
  • 4,034
  • 1
  • 20
  • 34
4

Here's one way to do this in java file:

textview.setText(Html.fromHtml("<b>" + title + "</b>" + "<small>" + description + "</small>"));

OR

Use spans.

Example:

final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");

// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); 

// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); 

// Set the text color for first 4 characters
sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

yourTextView.setText(sb);
Archit Goel
  • 694
  • 5
  • 19