0

I want to display an image into a TextView in Android and thought this would be possible with Html.fromHtml(). I found different tutorials where the image is a local resource. Is it also possible when I have an URL for the image? If yes, can someone give me an example?

Phil
  • 583
  • 4
  • 7
  • 19
  • Why would you want to load an image into a TextView? That's what ImageView is for. – Aleks G Apr 25 '13 at 12:30
  • image in text view ? isn't it a horrible decision ? – sumon Apr 25 '13 at 12:30
  • 1
    Because I read out a chat and there are smileys. So I want do something like `blablabla` smiley `blablabla` and therefor I need to display it into the textview – Phil Apr 25 '13 at 12:31
  • u need to use a view that will contain the text view and also some image view – sumon Apr 25 '13 at 12:32
  • Maybe this will help: http://stackoverflow.com/questions/8464506/how-to-display-an-image-from-an-url-within-textview – Daniel Apr 25 '13 at 12:34

2 Answers2

1

Instead of TextView you can use WebView for displaying image. If you need to display your smiley faces the you need to extract that keyword and replace with the specific image and draw it. For this you need to create your Custom View.

Pratik
  • 30,639
  • 18
  • 84
  • 159
1

I have solution for that. Put following code which add smiles in your text view.

private static final Factory spannableFactory = Spannable.Factory
        .getInstance();

private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

static {
    addPattern(emoticons, ":)", R.drawable.happy);
    addPattern(emoticons, ":(", R.drawable.sad);
    addPattern(emoticons, ":D", R.drawable.very_happy);
    // ...as many pattern you want. But make sure you have images in drawable directory
}

private static void addPattern(Map<Pattern, Integer> map, String smile,
        int resource) {
    map.put(Pattern.compile(Pattern.quote(smile)), resource);
}

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(),
                    matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()),
                        matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

public static Spannable getSmiledText(Context context, CharSequence text) {
    Spannable spannable = spannableFactory.newSpannable(text);
    addSmiles(context, spannable);
    return spannable;
}

After doing copy paste, write following code to set smiles in your text view.

textView.setText(getSmiledText(ActivityName.this,"Hi :) How are you...??? :D I am very :("));
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93