Textview wrap around View [closed]
Asked Answered
H

4

31

I'm trying to make my horizontal layouts take advantage of the room available.

In an info showing activity I have a 'fact box' followed by a large box of text. I'd like the infobox to float right, similar to the following picture.

Is this possible using the android TextView api?

Hartfield answered 2/9, 2010 at 11:45 Comment(3)
any alternatives to using a webview?Spook
I have found a better solution actually. The class StaticLayout will let you measure a string given a paint. This allows me to split the text up in three TextViews - top, side and bottom.Hartfield
Already answered [here][1] [1]: #2474512 Its a native widget which is ready to goSchlieren
W
3

I would suggest employing a WebView for this. You can format the text in a web view with the usual HTML / CSS formatting, with which your desired layout is pretty simple to accomplish.

Waterlogged answered 2/9, 2010 at 12:15 Comment(2)
Right, but it would be rather messy to share resources and stuff?Hartfield
I downvoted only because there's an easier and less messy solution with FlowTextView (see other answer)Tachycardia
G
11

found this solution!! check it out from source ---> answer source

How to have a few layout elements wrap around

Here is a problem people might encounter when dealing with layouts on android.

Say you want to place an image at the middle of a sentence. you may add a couple TextView and an ImageView but if the horizontal space gets too small the two TextViews are going to start wrapping around independently wich is going to look weird. You'd want the image to be part of your TextView and the entire sentence to wrap around nicely. To do so: * replace the three objects by one Spannable TextView * use a SpannableStringBuilder to append all your strings (don't forget to add an extra character (a space for example) that will be replaced by your image) * and finally use setSpan(ImageSpan, begin, end, flag) on that builder where ImageSpan is an ImageSpan containing the drawable of your picture, begin is the index of the character you've inserted

And here's what the code would look like:

add android:bufferType="spannable" to your TextView

SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(mContext.getText(R.string.part1));
int lengthOfPart1 = builder.length();
builder.append(" ");
builder.append(mContext.getText(R.string.part2));
Drawable d = mContext.getResources().getDrawable(R.drawable.picasaIcon);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); // <---- Very important otherwise your image won't appear
ImageSpan myImage = new ImageSpan(d);
builder.setSpan(myImage, lengthOfPart1, lengthOfPart1 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(builder);
Goldshlag answered 4/4, 2011 at 16:41 Comment(2)
It's a nice trick. Unfortunately spannable can only be used to position an image on a line. I guess an option could be to cut the image into a lot of line-height pieces, which could then be inserted as a spannable.Hartfield
Wow thank you so much for posting that source and this answer. I've been looking everywhere and never thought to use a Spannable. This should be the answer!Hydrostatics
Y
7

I know it's late but for people still trying to achieve this, here is a library which aims to achieve just this. https://github.com/deano2390/FlowTextView

Younglove answered 12/2, 2015 at 20:23 Comment(0)
W
3

I would suggest employing a WebView for this. You can format the text in a web view with the usual HTML / CSS formatting, with which your desired layout is pretty simple to accomplish.

Waterlogged answered 2/9, 2010 at 12:15 Comment(2)
Right, but it would be rather messy to share resources and stuff?Hartfield
I downvoted only because there's an easier and less messy solution with FlowTextView (see other answer)Tachycardia
M
2

I fear this is not possible with a single TextView. A TextView has to be an rectangle and is not able to display child views. A workaround could be to create two TextViews and make the first one cut of the text, then ask the first textview how much it can display and add the rest of the string to the second text view.

Mincemeat answered 2/9, 2010 at 11:56 Comment(2)
I couldn't find a method to check how much text can be displayed in a TextView. Maybe there is a way to do this but I couldn't find it now.Mincemeat
I know about the Paint.breaktext method. If I could get the Paint from the TextView, that could be doable.Hartfield

© 2022 - 2024 — McMap. All rights reserved.