How to Display Image in Android's Textview

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:how to display image in the textview

This worked for me,

Implementing a ImageGetter is straight and simple; you pass an ImageGetter object to be use to fetch the images that later will be use to fill the placeholders. In order to fetch the images the ImageGetter object must implement the getDrawable method. The getDrawable method must return the Drawable object that will fill the placeholder; but, if null is returned the generic image placeholder will be used instead (see implementation below).

For ImageGetter you need to override the method below:

public Drawable getDrawable (String  source)

To get images from the application resources first in the text file one inserts an html image tag like this:

<img src="my_image">

Note that "my_image" is just a name of a drawable not a path. Then use this code to diplay the text with images in TextView

  myTextView.setText(Html.fromHtml(myText, new ImageGetter() {                 
@Override
public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path = myActivity.this.getResources().getIdentifier(source, "drawable", "com.package...");
drawFromPath = (Drawable) myActivity.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
}, null));

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

Display TextView over ImageView in android

Try this instead:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />

<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />

</RelativeLayout>

How to display HTML img tag inside Android TextView

You should use WebView instead of TextView

   WebView = findViewById(R.id.WebView);
WebView.loadData(source, "text/html", "utf-8");

You will get the same output.

Show Image in TextView

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

}

Display images in between TextView

you can achieve same task with the help of ImageGetter class.
try below code:-

public class TestImageGetter extends Activity implements ImageGetter {
private final static String TAG = "TestImageGetter";
private TextView mTv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_image_getter);
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";

Spanned spanned = Html.fromHtml(source, this, null);
mTv = (TextView) findViewById(R.id.text);
mTv.setText(spanned);
}

@Override
public Drawable getDrawable(String source) {
LevelListDrawable d = new LevelListDrawable();
Drawable empty = getResources().getDrawable(R.drawable.ic_launcher);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

new LoadImage().execute(source, d);

return d;
}

class LoadImage extends AsyncTask<Object, Void, Bitmap> {

private LevelListDrawable mDrawable;

@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
Log.d(TAG, "doInBackground " + source);
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
Log.d(TAG, "onPostExecute drawable " + mDrawable);
Log.d(TAG, "onPostExecute bitmap " + bitmap);
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setLevel(1);
// i don't know yet a better way to refresh TextView
// mTv.invalidate() doesn't work as expected
CharSequence t = mTv.getText();
mTv.setText(t);
}
}
}
}

see below link and i can achieve same task from help of below link :-

Html.ImageGetter TextView

Show image from HTML tag in text view?

You can use this code for set html image and text both from textview:

This is xml code:

      <TextView
android:id="@+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/black" />

This is Java code:

public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
private TextView txtDescription;
private Drawable empty;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_condition_use);
txtDescription = (TextView) findViewById(R.id.txtDescription);
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";
Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
txtDescription.setText(spanned);
}

@Override
public Drawable getDrawable(String s) {
LevelListDrawable d = new LevelListDrawable();
empty = getResources().getDrawable(R.drawable.splash1);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(s, d);
return d;
}

class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;

@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
Log.d(TAG, "doInBackground " + source);
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
//mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
mDrawable.setLevel(1);
CharSequence t = txtDescription.getText();
txtDescription.setText(t);
}
}
}


Related Topics



Leave a reply



Submit