user971511
user971511

Reputation: 167

Android. How to insert image in text

Actually I have TextView with some text. I need to insert an image insight this text. Please suggest the best way to do that. Should be supported on different display sizes.

Upvotes: 1

Views: 1411

Answers (3)

Rohit Sharma
Rohit Sharma

Reputation: 13815

If want to do it via xml

add this attribute to TextView element

android:background="@drawable/icon"

If at runtime programatically

    TextView tv = (TextView)findViewById(R.id.textView);
    tv.setBackgroundDrawable(drawable);

            or

    tv..setBackgroundResource(resid);

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Are you talking about to display image inside the TextView at either Top/Bottom/Right/Left side, if yes then:

<TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, DemoExampleActivity!"
        android:drawableLeft="@drawable/icon"
        android:drawableRight="@drawable/icon"
        android:drawablePadding="10dip"/>

output:

enter image description here

Upvotes: 1

Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

You can set the image as the background for the TextView like this:

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Text to be displayed"
    android:background ="@drawable/your_image"
    />

Good luck, Arkde

Upvotes: 0

Related Questions