8

Is there anyway to wrap a TextView around an image? It's the typical thing that people do in CSS like this http://www.echoecho.com/htmlimages08.htm

Thanks,
Tee

teepusink
  • 27,444
  • 37
  • 107
  • 147
  • If you want to use only the left floating and for Android phones starting from the version 2.2, you can use the solution which I've described at my answer to another question here: http://stackoverflow.com/a/8463221/427225 – vortexwolf Dec 11 '11 at 10:25

3 Answers3

15

I've written a widget for this: http://code.google.com/p/android-flowtextview/

enter image description here

This widget extends (and therefore behaves like) a RelativeLayout so you can add child views. The class exposes a setText() method which allows you to set the view's text (plain or spannable) and then you call invalidate() to render the text. The text will fill in any space left by the child views.

Example usage:

 <com.pagesuite.flowtext.FlowTextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:padding="10dip"
                android:src="@drawable/android" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="400dip"
                android:padding="10dip"
                android:src="@drawable/android2" />
        </com.pagesuite.flowtext.FlowTextView>

Then in your code:

tv = (FlowTextView) findViewById(R.id.tv);              
Spanned spannable = Html.fromHtml("<html ... </html>");
tv.setText(spannable);  // using html
tv.setText("my string"); // using plain text    
tv.invalidate(); // call this to render the text

Always happy to assist if you have any problems using this, find my email on my profile

Dean Wild
  • 5,886
  • 3
  • 38
  • 45
  • How fast is this? Does it use WebView inside? – Thomas Ahle Feb 05 '13 at 17:45
  • I don't want scrollview in this library instead I want to ellipsize text. Any solution ? – Vishal Khakhkhar Apr 10 '13 at 10:21
  • What if I click a picture from my device and want that to appear as a thumbnail along with other text bits? I tried the above code and it throws an error saying it needs just drawables. Is there a way around the same? I want to simply set my bitmap image in the imageView and want the text to flow around it? Any help appreciated! :] – Garima Tiwari May 16 '13 at 09:38
  • Garima send me your code and i'll take a look - dw86@kent.ac.uk – Dean Wild May 17 '13 at 11:32
  • @DeanWild, I worked around my code for a while and found a solution so thats sorted, also I tried the code you've posted above and it works beautifully but i was looking for something more visually impressive. I need my text to be wrapped around my thumbnail along with a button as a footer. Any tips? – Garima Tiwari May 27 '13 at 04:33
4

Why not just use a WebView? You'll have more freedom in the future to customize the layout that way.

If a WebView is too heavyweight for your use case, you'll probably need to render the text and image manually. You may find some relevant information in this android-developers thread.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
0

Another way to accomplish this is to make a image containing the text and the images you want displayed. Obviously heavier weight and a workaround.

You can do this using webview and a workaround to load local content. As of the 1.0 SDK the webview cant load local content without the work arounds described in these articles:

http://www.techjini.com/blog/2009/01/10/android-tip-1-contentprovider-accessing-local-file-system-from-webview-showing-image-in-webview-using-content/

and

http://blog.tourizo.com/2009/02/how-to-display-local-file-in-android.html

Roman's link is to a thread that says you can't do it with text view, so that's not an option at this point.

Ravedave
  • 1,158
  • 2
  • 11
  • 24