14

I have a layout that looks something like this:

[TextView 1] [TextView 2]
[ TextView 2 spill-over ]

Essentially, I need the contents of TextView 2 to wrap to the next line, but start where TextView 1 starts. I was thinking that if I knew how much text would fit into TextView 2 before it runs out of space on line one, I could take the rest of the text and put it in another TextView below the first two. So I need to measure how much text will fit into a TextView (which can be tricky because as far as I can tell, Android will try to break the text in a TextView at a good location so that it won't break a word in the middle if it can be avoided) or I need a new idea on how to lay this out.

Any help would be greatly appreciated.

Thanks in advance,

groomsy

groomsy
  • 4,945
  • 6
  • 29
  • 33
  • If this is possible, I have a feeling it will be a lot more complicated that it will be worth. Why can't you just append your TextView2's text to textview 1? – Falmarri Aug 04 '10 at 20:02
  • 1
    Different formatting. Plus, TextView 1 can sometimes be an ImageView. I've thought about putting everything in a WebView, but I don't know how I would stop at 2 lines though. – groomsy Aug 04 '10 at 20:43
  • http://stackoverflow.com/questions/7259016/scale-text-in-a-view-to-fit/7259136#7259136 – Ron Mar 19 '14 at 12:16

3 Answers3

12

Unfortunately, Paint.breakText didn't return the exact same result as in was seen in my two-line TextView.

However, this worked

int numChars = textView.getLayout().getLineEnd(1);

(use numberOfLines - 1 as the parameter to it)

Ref http://developer.android.com/reference/android/text/Layout.html#getLineEnd(int) Set individual lines of TextView to different width

Community
  • 1
  • 1
dparnas
  • 4,090
  • 4
  • 33
  • 52
  • This got me going down the right path with a related problem. A combination of `getLineCount` and `getLineStart` did the trick for me when custom trimming/"ellipsizing" a TextView. – loeschg Mar 28 '14 at 19:19
  • Also, for anyone having an issue with `getLayout()` returning null, check this question. http://stackoverflow.com/questions/19157650/how-to-get-height-of-view-inside-adapter-for-creating-sized-bitmap – loeschg Mar 28 '14 at 19:20
  • 3
    @Ioeschg `getlayout()` returns `null`, but the above given link is not helpful. Can you tell what's going wrong? – Ammar Jul 14 '14 at 05:20
  • 1
    how is this accepted answer, getting null in getLayout(). – Parth Anjaria Oct 26 '18 at 06:43
6

You can create a Paint object with TextView2's text size and use breakText() to measure how many characters will fit in your TextView2's width.

(This is untested code - might need some slight modifications)

String textToBeSplit = arbitraryText; // Text you want to split between TextViews
float textView2Width = somehowGetItsWidth; // TextView2's width
float myTextSize = textView2.getTextSize();

Paint paint = new Paint();
paint.setTextSize(myTextSize); // Your text size
int numChars = paint.breakText(textToBeSplit, true, float textView2Width, null);

numChars tells you how many characters in textToBeSplit will fit in TextView2's width, enabling you to split it between your views.

Andy Zhang
  • 8,285
  • 1
  • 37
  • 24
  • Thank you for the rough example. I will work on this and let you know how it turns out. – groomsy Aug 04 '10 at 21:53
  • 1
    Tried this and it almost worked. The problem was that the TextView uses a slightly different approach, especially when it has more than one line. The count would be 2-3 characters of. Found a better solution that I'll post as an answer – dparnas Sep 11 '12 at 18:51
  • @dparnas hey did find answer for "The count would be 2-3 characters of" – ShreeshaDas Jun 12 '13 at 05:53
  • make sure textview is drawn with text, this can be achieved with using `.post{}` – Mahmoud Mabrok Apr 03 '22 at 14:15
2

You don't need two TextViews in order to do this, you should always use as few views as possible and you can use a spannable in order to have two styles for the same textview.
For example with :

TextView tv = (TextView) findViewById(R.id.textview);
SpannableString text = new SpannableString(myString);

text.setSpan(new TextAppearanceSpan(getContext(), R.style.myStyle), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(getContext(), R.style.myNextStyle), 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(text, TextView.BufferType.SPANNABLE);
Teovald
  • 4,369
  • 4
  • 26
  • 45