wood burning stoves 2.0*
The moose likes Android and the fly likes TextView autoLink Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "TextView autoLink" Watch "TextView autoLink" New topic
Author

TextView autoLink

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Hi,

When using TextView autoLink xml attribute, is it possible to use html elements?

I tried it, of course, but it did not work for me.
The Text gets rendered as a link, but it is not clickable.
Having just a link, say
http://google.com or google.com without using tags works fine.

What I want to do, for example, is to show the text "Google" as a clickable link to
http://google.com only using the xml definitions.

Cheers.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Ed Burnette
Author
Ranch Hand

Joined: Jun 10, 2003
Posts: 142
The documentation says that only a few HTML tags are supported:
http://developer.android.com/guide/topics/resources/available-resources.html#stringresources

The trick is that your text string MUST come from a resource and not from a static string in your code.

If you look at the source code you'll see that more should be possible than just the ones that are documented:

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/Html.java;h=90f5e4c683a1769c57757e21a51e2e794ed6fdf7;hb=HEAD

I see support for in the source so try it and see if it works.


Ed Burnette, Author of Hello Android
Blog: ZDNet's Dev Connection - Twitter: @eburnette
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Thanks, Ed.

The string is coming from a resource.
From the documentation, I picked up one thing that I wasn't doing - escaping quotes.
I fixed that, but that didn't have any effect.

The source code you linked to shows handling of tags.
And true enough, the tag gets rendered as a link, it is a different color and underlined.
But, you can't click it to launch the browser.

Cheers.
Ed Burnette
Author
Ranch Hand

Joined: Jun 10, 2003
Posts: 142
Hmm, it wouldn't work for me either. I guess that's why it's not documented.

It looks like for now you'll have to either live with the plain linkifying, or use a WebView. WebViews are nice because you can do images, text flow, tables, and all that. You can even have mixed Java/Android + JavaScript/HTML applications where the two halves are displayed on the screen seamlessly and communicate with each other. See "Section 7.3, From JavaScript to Java and Back", and try the LocalBrowser example code from the web site.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Ed Burnette wrote:
See "Section 7.3, From JavaScript to Java and Back"


I am right at that chaper ha ha, I actually had the book open at that page as I started to read this post!

I fudged around with the code a little.
As the following code demonstrates, the links are created, but they are just not made clickable for some reason.

Hello.java


strings.xml


main.xml


Anyway, this was just a curiosity. May be, I will check it again when a newer version of Android comes out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TextView autoLink