How to make html image clickable inside a TextView

Posted by Gonan on Stack Overflow See other posts from Stack Overflow or by Gonan
Published on 2012-06-15T00:47:50Z Indexed on 2012/06/23 15:16 UTC
Read the original article Hit count: 242

Filed under:
|
|

I have the following text in a string in the resources file:

    <a href="mailto:[email protected]">&lt;img src="mail_big" /&gt;</a>

It shows the image fine (I implemented ImageGetter) but it is not clickable. I have tried adding the Linkify thingy but I don't think it's meant for this case, and so it doesn't work. The setMovementMethod doesn't work either.

I have tried different combinations of the above:

    <a href="mailto:[email protected]">&lt;img src="mail_big" /&gt;hello</a>

Here, even the "hello" part is not clickable (neither blue nor underlined).

    <a href="mailto:[email protected]"><img src="mail_big" /></a>

This doesn't even show the image.

    &lt;a href="mailto:[email protected]"&gt;&lt;img src="mail_big" /&gt;&lt;/a&gt;

If I just write the email, without the <a> tag it works perfectly, but I would like to use the image of an envelope that the user can click on. It's not possible to use an imagebutton because this text is in the middle of a string and so I can't split it.

Any ideas?

Thanks!

EDIT:

I found a solution or rather found how to do it correctly.

All I had to do was adding the setMovementMethod call before the call to setText in the TextView and ALSO, and COMPLETELY NECESSARY, remove the attribute "android:autoLink="all" from the layout. Apparently, parsing mails and urls in a string is mutually exclusive to interpreting the link tags in a string. So one or the other but not both.

Finally my layout is just a TextView with nothing special, just width and height. The activity is like this:

    TextView tv = (TextView)findViewById(R.id.about_text);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setText(Html.fromHtml(getString(R.string.about_content), new ImageGetter(), null));

And the string is like this:

    <string name="about_content"><a href="mailto:[email protected]"><img src="mail" /></a></string>

© Stack Overflow or respective owner

Related posts about android

Related posts about html