Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Image in TextView

I wish to show Images in TextView. My Images are saved in res/raw directory. I tried using HTML.ImageGetter but could not find a complete reference for same.

like image 459
Gaurav Agarwal Avatar asked Nov 18 '11 15:11

Gaurav Agarwal


1 Answers

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class TextImageActivity extends Activity {
    int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tvText = (TextView) findViewById(R.id.text);
    final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
            + "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
                    " in a more elaborate</body></html>";
    tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}

  private ImageGetter imgGetter = new ImageGetter() {

    public Drawable getDrawable(String source) {
            Drawable drawable = null;
            if(imageNumber == 1) {
            drawable = getResources().getDrawable(R.raw.icon);
            ++imageNumber;
            } else drawable = getResources().getDrawable(R.raw.a);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                                    .getIntrinsicHeight());

            return drawable;
    }
 };

 }
like image 175
Gaurav Agarwal Avatar answered Oct 31 '22 18:10

Gaurav Agarwal