Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display image in Android's TextView?

Tags:

android

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

like image 911
indira Avatar asked Apr 06 '11 05:04

indira


People also ask

How to set image in textview in android studio?

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

What is image button in Android Studio?

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states.


2 Answers

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); 
like image 59
Buda Gavril Avatar answered Nov 11 '22 10:11

Buda Gavril


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); 
like image 38
Dawid Avatar answered Nov 11 '22 09:11

Dawid