views:

109

answers:

2

Is there a way to make my textview wrap around other views? For example in the picture, is there a way to get it to wrap to the edge once it gets below the imageview?

alt text

This is my xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
android:background="@android:color/white">


    <LinearLayout android:id="@+id/AUTHOR_TITLE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:layout_alignParentTop="True">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:text="Author Name"
            />

    </LinearLayout>
        <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"

android:layout_height="wrap_content"    
android:layout_below="@id/AUTHOR_TITLE">


            <ImageView android:id="@+id/PICTURE"
            android:layout_alignParentLeft="True"
            android:layout_alignParentTop="True"
            android:src="@drawable/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  />
                    <TextView android:id="@+id/DESCRIPTION" 
            android:layout_toRightOf="@id/PICTURE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF OMGTESTLOLWTF
OMGTESTLOLWTF "/>   
        </RelativeLayout>

</RelativeLayout>

I'm really surprised that no one's done this yet =\ Sigh.

+2  A: 

This is not currently possible with the built in widgets. You can roll your own, it shouldn't be too hard. You'd need to extend one of the ViewGroup classes and add the images and text to that. Then, in onLayout() you can figure out how to flow the text so that it wraps the images.

CaseyB
can you give a more steps?
pengwang
A: 

There's no way to what you want, have you thought about having two TextViews?

Like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content" android:id="@+id/layout"
  android:layout_height="wrap_content">

  <ImageView android:id="@+id/image" android:layout_alignTop="@id/layout"
  android:layout_width="wrap_content" android:layout_height="wrap_content" />

  <TextView android:id="@+id/toptext" android:layout_toRightOf="@id/image" 
  android:layout_width="wrap_content" android:layout_height="wrap_content" />

  <TextView android:id="@+id/maintext" android:layout_below="@id/image" 
  android:layout_width="wrap_content" android:layout_height="wrap_content" />

</RelativeLayout>

Then, get the height and width of the image, calculate the amount of text you can have in toptext (by getting the height/font size and width/text width of 1 character, put the rest in maintext.

raybritton
Basing it on character width only works for monospaced fonts, and the calculation would have to take word wrap into account. Essentially, it would be a reimplementation of the guts of the widget.
Blrfl
@Blrfl Yes, but it would only be 10 lines or so (I think, I did something similar for drawing text on to the Canvas) and it's easier than creating a new widget that supports images and text and then adding this code anyway.
raybritton