How to Add Image in a Textview Text

How to add image in a TextView text?

Try this ..

    txtview.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.image, 0, 0, 0);

Also see this.. http://developer.android.com/reference/android/widget/TextView.html

Try this in xml file

    <TextView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/image"
android:drawablePadding="5dp"
android:maxLines="1"
android:text="@string/name"/>

Append image to TextView

I have found the solution now for Api 21, 22, 23+:

private void appendImage(Bitmap bmp)
{
tv.setTransformationMethod(null);
SpannableString ss = new SpannableString(" ");
ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(ss);
}

And in the XML:

android:textAllCaps="false"

The error was that in api level 21+ you have to set the TransformationMethod null.

How to display image in Android's TextView?

You can create a spannableString and place your image where you want in the TextView. Or you can use

ImageSpan is = new ImageSpan(context, resId);
text.setSpan(is, index, index + strLength, 0);

Android Add image to text (in text View)?

I think you are looking for the Spannable interface, By using this you can add images to a text view.

This link might help.

Sample Image

Add multiple image in textview

----------------- add_expenses.xml -------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<LinearLayout
android:layout_width="320dp"
android:layout_height="50dp"
android:background="@drawable/rounded_corner_textview"
android:layout_gravity="center_vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/black"
android:textSize="38sp"
android:text="Receipt"
android:drawableLeft="@drawable/recepit"
android:drawableRight="@drawable/forward_arrow"
android:drawablePadding="10dp" />

</LinearLayout>

</LinearLayout>

---------------- rounded_corner_textview.xml ------------------

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="3dp"
android:color="@android:color/black"/>
<corners android:radius="3dp"/>

</shape>

------------------ forward_arrow.xml --------------------------

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/forward_arrow"
android:tint="@android:color/black"
/>

------------------recepit.xml--------------------------

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/recepit"
android:tint="@color/blue"
/>


Related Topics



Leave a reply



Submit