57

I want to use an ImageView to show some message in a fancy way.

How do I add text to an ImageView?

9 Answers 9

98

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>
4
  • 9
    PS.: Must be inside a RelativeLayout
    – Alesqui
    Jun 8, 2012 at 16:08
  • 2
    better include this PS in post. It doesn't work without your PS :)
    – mente
    Jul 25, 2013 at 12:54
  • 7
    This is not recommended as it creates addition layouts. Use TextView as suggested in other answers.
    – Tautvydas
    Nov 17, 2014 at 0:51
  • 2
    I had to use this, but switching to TextView solved it without any trouble much simpler, I guess. Sep 29, 2017 at 12:04
34

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" />
2
  • 3
    the problem with this is that background cant be scaled . Apr 30, 2014 at 8:18
  • 1
    @IgorGanapolsky: what?! it's available on ALL API Levels, starting from 1. Sep 25, 2014 at 8:03
25

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

Use drawalbeLeft/Right/Bottom/Top in TextView to render image at respective position.

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/image"    
        android:text="@strings/text" 
        />
2
  • 5
    Image will be adjacent to, but not on top of the TextView. Aug 7, 2014 at 20:14
  • best answer is this, using drawableTop, drawableBottom, ... in TextView
    – Obtice
    Oct 22, 2019 at 7:05
11

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.

2
  • What is android:drawableLeft for here? Aug 7, 2014 at 20:20
  • 1
    I have added some comments above.
    – Andrew S
    Aug 8, 2014 at 21:18
6

To draw text directly on canvas do the following:

  1. Create a member Paint object in myImageView constructor

    Paint mTextPaint = new Paint();
    
  2. 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.

1
  • would you please help me regarding the reverse way , I mean how to get text of existing ImageView (which is drawn in above way )in Android?
    – aida
    Apr 26, 2016 at 8:45
3

With a FrameLayout you can place a text on top of an image view, the frame layout holding both an imageView and a textView.

If that's not enough and you want something more fancy like 'drawing' text, you need to draw text on a canvas - a sample is here: How to draw RTL text (Arabic) onto a Bitmap and have it ordered properly?

2

You can use the TextView for the same purpose, But if you want to use the same with the ImageView then you have to create a class and extends the ImageView then use onDraw() method to paint the text on to the canvas. for more details visit to http://developer.android.com/reference/android/graphics/Canvas.html

2
    <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>
1
  • Dont worry just use image button under FRAME LAYOUT and drag TEXT VIEW on image button .Done If u like this post useful visit my site www.krsolutions.in and click on like button on bottom of the site May 24, 2013 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.