I want to inset some images in the TextView. How to do it? Any idea
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);
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With