deepthi
deepthi

Reputation: 8487

how to keep text on imageview in android?

i want to display text on image view.

Upvotes: 3

Views: 13213

Answers (9)

Alê Oliveira
Alê Oliveira

Reputation: 6439

That's how I did it and it worked exactly as you asked for:

<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" />

PS: Only works inside a RelativeLayout

Upvotes: 1

Sagar V.
Sagar V.

Reputation: 3637

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" />

Upvotes: 1

syp_dino
syp_dino

Reputation: 395

I stuck together several ways, and that's work for me, with the text centered in the image inside RelativeLayout.

<RelativeLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:contentDescription="line"
    android:src="@drawable/tile1" />

<TextView
    android:id="@+id/grid_item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:text="Line 1"
    android:textColor="#FFFFFF"
    android:textSize="18sp"
    android:textStyle="bold" />
 </RelativeLayout> 

Upvotes: 0

Vishal
Vishal

Reputation: 20627

You can also try just TextView with image

<TextView
    app:layout_gravity="center_horizontal"
    android:drawableTop="@drawable/icon"
    android:gravity="center_horizontal"
    android:text="Icon"
    android:textSize="20sp"
    android:textStyle="bold" >
</TextView>

Upvotes: 0

bradley4
bradley4

Reputation: 3938

This worked for me inside a RelativeLayout

<ImageView 
    android:id="@+id/image_ct" 
    android:src="@drawable/categories_bill" 
    android:scaleType="center"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/>

<TextView 
    android:id="@+id/catdescription_ct" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Sample Description"
    android:layout_alignTop="@id/image_ct" 
    android:textColor="#000000" />

Upvotes: 4

Tristan Warner-Smith
Tristan Warner-Smith

Reputation: 9771

Not sure how much this would apply but if you're trying to do this (pseudo code):

<LinearLayout>
    <TextView />
    <ImageView />
</LinearLayout>

As you're discovering, you'll have issues doing that, if you change it to be this though:

<LinearLayout Background="YourImagePath">
    <TextView />
</LinearLayout>

You'll probably find that easier.

Assuming that you're not after creating a derived type and painting yourself as others are suggesting.

Upvotes: 4

ChristianS
ChristianS

Reputation: 63

Couldn't he just put an ImageView and a TextView in a RelativeLayout and set the layout_align properties of one of them to reference the other view so they will simply overlap each other or am I missing something? Seems more simple to me.

Upvotes: 1

Maurits Rijk
Maurits Rijk

Reputation: 9985

You can create a new class derived from ImageView. Override the onDraw method, drawing the parent class first and then your own text.

Upvotes: 1

reflog
reflog

Reputation: 7645

you'll have to subclass it and overload the onDraw method, then draw on the graphic context using the paintString method.

or just overlay a label over the ImageView

Upvotes: 1

Related Questions