4

I've ran into a bit of an issue as I'd like two TextViews to layout side-by-side even if the first TextView has several lines of text. Ideally, the next TextView should simply follow right after. It's a bit hard to explain, so I've made this picture to illustrate it:

(the first TextView contains a title, and the second TextView contains a year)

I've been trying a few different things now, both layout hacks and using Html.fromHtml(), but nothing has worked so far. It's probably impossible to do, but I just wanted to be sure.

Michell Bak
  • 13,182
  • 11
  • 64
  • 121

2 Answers2

5

This sounds like a job for Spannable. As you've noticed the problem with doing two views is that the bounds of the TextView in your two line example is still a rectangle, so laying out the second view at the end of the text gets tricky. HTML can work for simple tags (bold, underline, headings), but isn't too flexible.

The solution is to use Spannable to create the text ouf of pieces of content with specific formatting applied to each "span". As it turns out, then you can just stick the whole results right into a single TextView. Something like:

TextView tv; //Defined somewhere else

SpannableStringBuilder resultBuilder = new SpannableStringBuilder();
SpannableString item = new SpannableString("Long and Fancy Movie Title Here");
item.setSpan(new AbsoluteSizeSpan(18, true), 0, item.length(), 0);
resultBuilder.append(item);

SpannableString item = new SpannableString("(2011)");
item.setSpan(new AbsoluteSizeSpan(12, true), 0, item.length(), 0);
resultBuilder.append(item);

tv.setText(resultBuilder, TextView.BufferType.SPANNABLE);

There are a ton of different span definitions in android.text.style, the above is just an example to create two different text sizes in the same string. You should be able to get what you want out of this one, TextAppearanceSpan, TypefaceSpan, or some other combination.

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
1

Two side by side TextView in a RelativeLayout might do the trick, I don't see why the Html.fromHtml wont work if you add the right html formatting

akshaydashrath
  • 1,025
  • 8
  • 18
  • 1
    A RelativeLayout doesn't work. It'll move the second TextView to the right of the first TextView. That can leave a huge gap between the first and second, if the first has more than one line of text. As for `Html.fromHtml()` I was using `(2011)` for the second part, and it completely dismissed the `small`-tag. – Michell Bak Jun 19 '12 at 23:26
  • This might help. http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview – akshaydashrath Jun 19 '12 at 23:29
  • Yeah, I've been looking at that, but like I said I couldn't get the `small`-tag to work. – Michell Bak Jun 19 '12 at 23:33
  • Strange, I was following this guide http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html Do you suppose it could be device related, have you tried on another device? – akshaydashrath Jun 19 '12 at 23:35
  • another possible workaround http://stackoverflow.com/questions/7388361/textview-with-different-textsize – akshaydashrath Jun 19 '12 at 23:37
  • Spannable seems to get the job done, and Devunwired posted a complete solution, so I'm accepting that, but +1 for your comments as well! – Michell Bak Jun 19 '12 at 23:45