How to display image in Android's TextView?

Android

Android Problem Overview


I want to inset some images in the TextView. How to do it? Any idea

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

Much easier you can use SpannableStringBuilder from API 1

Example usage:

For API >= 21

    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append("My string. I ")
			.append(" ", new ImageSpan(getActivity(), R.drawable.ic_action_heart), 0)
			.append(" Cree by Dexode");

	textView.setText(builder);

For API >= 1

	SpannableStringBuilder builder = new SpannableStringBuilder();
	builder.append("My string. I ").append(" ");
	builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_action_heart),
			builder.length() - 1, builder.length(), 0);
	builder.append(" Cree by Dexode");

	textView.setText(builder);

Solution 3 - Android

ImageSpan is = new ImageSpan(context, R.drawable.arrow);
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
text.setSpan(is, 5, 5 + 10, 0);

Solution 4 - Android

do something like this.

textView.setCompoundDrawableWithIntrinsicBounds(yourImg, null, null, null);

Solution 5 - Android

make custom.xml

       <RelativeLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
          android:layout_height="wrap_content">
          <ImageView 
               android:id="@+id/thumbnail_view"
               android:src="@drawable/ic_launcher"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />

    <TextView android:id="@+id/message_view"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_toRightOf="@id/thumbnail_view"
               android:textSize="18sp"
               android:text="MyText" />
      </RelativeLayout>

then in main.xml , include this custom.xml

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" 
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"  
          android:gravity="center_horizontal">

   <include 
        android:id="@+id/customView"
         layout="@layout/custom"/>

           </LinearLayout>

This is my mainActivity.class

  package com.example.test;


 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ImageView;
  import android.widget.TextView;


     public class MainActivity extends Activity implements OnClickListener {

   private String TAG = MainActivity.class.getSimpleName();
  ImageView img;
  ImageView img1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	
	TextView txt = (TextView)findViewById(R.id.message_view);
	 img = (ImageView) findViewById(R.id.thumbnail_view);
	 img1 = (ImageView) findViewById(R.id.thumbnail_view1);
	
	img.setOnClickListener(this);
	img1.setOnClickListener(this);
	
}
@Override
public void onClick(View v) {
	if(v== img){
		// do something for img
	}
	else if (v== img1){
		//do something for img1
	}
	
}
}

Solution 6 - Android

If you want to add the image end of the view you can follow me

  ImageSpan is = new ImageSpan(context, R.drawable.arrow);
  SpannableString text = new SpannableString("Lorem ipsum dolor sit amet"+"  ");
  text.setSpan(is, text.length()-2, text.length(), 0);

if Image need to center alignment you can follow

ImageSpan center vertical

recommended @amy answer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionindiraView Question on Stackoverflow
Solution 1 - AndroidBuda GavrilView Answer on Stackoverflow
Solution 2 - AndroidGelldurView Answer on Stackoverflow
Solution 3 - AndroidPranita PatilView Answer on Stackoverflow
Solution 4 - AndroidAjay SinghView Answer on Stackoverflow
Solution 5 - AndroidPrachiView Answer on Stackoverflow
Solution 6 - AndroidTariqulView Answer on Stackoverflow