I want to use an ImageView
to show some message in a fancy way.
How do I add text to an ImageView
?
To draw text directly on canvas do the following:
Create a member Paint object in myImageView
constructor
Paint mTextPaint = new Paint();
Use canvas.drawText
in your myImageView.onDraw()
method:
canvas.drawText("My fancy text", xpos, ypos, mTextPaint);
Explore Paint and Canvas class documentation to add fancy effects.
To add a text to your ImageView
you can do this:
<RelativeLayout> // Only works inside a RelativeLayout
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myImageView"
android:layout_alignTop="@+id/myImageView"
android:layout_alignRight="@+id/myImageView"
android:layout_alignBottom="@+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
You can also use a TextView and set the background image to what you wanted in the ImageView
. Furthermore if you were using the ImageView
as a button you can set it to click-able
Here is some basic code for a TextView
that shows an image with text on top of it.
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/your_image"
android:text="your text here" />
I know this question has been and gone, but if anyone else stumbled across this I wanted to let them know. This may sound an unintuitive thing to do but you could use a button with clickable set to false or what ever. This is because a button allows one to set drawableLeft, drawableRight, drawableTop etc in addition to text.
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_box1"
android:drawableLeft="@drawable/ar9_but_desc"
android:padding="20dp"
android:text="@string/ar4_button1"
android:textColor="@color/white"
android:textSize="24sp" />
New Info: A button can have icons in drawableLeft, drawableRight, drawableTop, and drawableBottom. This makes a standard button much more flexible than an image button. The left, right, top etc is the relation to the text in the button. You can have multiple drawables on the button for example one left, one right and the text in the middle.
Best Option to use text and image in a single view try this:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/ic_launcher"
android:text="TextView" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="30" >
<ImageButton
android:id="@+id/imgbtnUploadPendingPods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"
android:src="@drawable/upload_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="30dp"
android:text="@string/pendingpods"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>