Cranosaur Cranosaur - 3 months ago 25
Android Question

How to add image in a TextView text?

I'm new to Android app development and coding and I've spent almost my whole day trying to figure this out.

I've searched around on Google and came across this site where I found a question similar to mine in which how to include a image in a

TextView
text, for example "hello my name is [image]", and the answer was this:

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


Like I said I just got into Android app development so I'm still learning a lot but I would like to know in this code,


  1. What am I supposed to type or do in the context?

  2. Am I supposed to do something to the
    text.setSpan()
    like import or reference or leave it text?



If someone can break this down for me that would be much appreciated. I've been getting the hang of this for the last 2 weeks but I'm still learning a lot.

Thanks in advance!

Answer

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:singleLine="true"
        android:text="@string/name"/>
Comments