Search code examples
javaandroidxmlimagetextview

How to add image in a TextView text?


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);

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.


Solution

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