1

I have been searching online but haven't managed to find something that I could use. I have text in my textview that will have text something like;

1 Person
2 Person
3 Person

the text is formatted like this (1\nperson), (2\nperson) .... so adding space between the number and the person string.

What I want to do is make the number of each string bigger. For example;

enter image description here

I have searched online and the links / similar things I found are not that helpful:

Is it possible to have multiple styles inside a TextView?

TextView with different textSize

The default font size for the textview is 15, so the person string font size will always be 15, what I want to do is make the numbers bigger, for example to font size 20.

Community
  • 1
  • 1
Henry
  • 1,042
  • 2
  • 17
  • 47

2 Answers2

4

I think this would work:

Spannable span = new SpannableString(textView.getText());
span.setSpan(new RelativeSizeSpan(1.5f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
williamj949
  • 11,166
  • 8
  • 37
  • 51
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • It works! please could you tell me what 1.5f, 0, 1, refers to. – Henry Jan 22 '16 at 22:32
  • Check out the Android docs: [RelativeSizeSpan](http://developer.android.com/intl/ja/reference/android/text/style/RelativeSizeSpan.html#RelativeSizeSpan(float)) and [Spannable](http://developer.android.com/intl/ja/reference/android/text/Spannable.html). `1.5f` is the relative size of the text -- so "make it 1.5x as big as the rest of the text" `0, 1` is the start/end range -- so only change the first character – Buddy Jan 22 '16 at 22:34
  • Quick question, is there a solution to check if the number is a decimal number e.g. 1.6 then to change the size of this number to. – Henry Jan 22 '16 at 22:42
  • Yeah, you can give variables for the start/end instead of hard-coding it like I did in this example. For example you could set end to `textView.getText().toString().indexOf('\n')` – Buddy Jan 22 '16 at 22:44
3

Why don't you use two TextViews inside a LinearLayout instead of one TextView? It will do what you need. You would put the first character in the first TextView and the rest of the String in the second TextView. If you need help doing that let me know.

Nouvel Travay
  • 6,292
  • 13
  • 40
  • 65