Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - UI for text wrap an image

Tags:

android

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

like image 820
teepusink Avatar asked Mar 18 '10 23:03

teepusink


2 Answers

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

like image 163
Dean Wild Avatar answered Oct 13 '22 17:10

Dean Wild


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.

like image 40
Roman Nurik Avatar answered Oct 13 '22 15:10

Roman Nurik