views:

2793

answers:

4

I'm currently rendering HTML input in a TextView like so:

tv.setText(Html.fromHtml("<a href='test'>test</a>"));

The HTML being displayed is provided to me via an external resource, so I cannot change things around as I will, but I can, of course, do some regex tampering with the HTML, to change the href value, say, to something else.

What I want is to be able to handle a link click directly from within the app, rather than having the link open a browser window. Is this achievable at all? I'm guessing it would be possible to set the protocol of the href-value to something like "myApp://", and then register something that would let my app handle that protocol. If this is indeed the best way, I'd like to know how that is done, but I'm hoping there's an easier way to just say, "when a link is clicked in this textview, I want to raise an event that receives the href value of the link as an input parameter"

+4  A: 

I found the answer myself, over here:

http://blog.elsdoerfer.name/2009/10/29/clickable-urls-in-android-textviews/

It seems that'll just have to do.

David Hedlund
+2  A: 

Coming at this almost a year later, there's a different manner in which I solved my particular problem. Since I wanted the link to be handled by my own app, there is a solution that is a bit simpler.

Besides the default intent filter, I simply let my target activity listen to ACTION_VIEW intents, and specifically, those with the scheme com.package.name

<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="com.package.name" />  
</intent-filter>

This means that links starting with com.package.name:// will be handled by my activity.

So all I have to do is construct a URL that contains the information I want to convey:

com.package.name://action-to-perform/id-that-might-be-needed/

In my target activity, I can retrieve this address:

Uri data = getIntent().getData();

In my example, I could simply check data for null values, because when ever it isn't null, I'll know it was invoked by means of such a link. From there, I extract the instructions I need from the url to be able to display the appropriate data.

David Hedlund
A: 

its very simple add this line to your code: tv.setMovementMethod(LinkMovementMethod.getInstance());

jonathan
Thanks for your reply, jonathan. Yes, I knew about MovementMethod; what I wasn't sure about was how to specify that my own app should handle the link click, rather than just opening a browser, as the default movement method would (see the accepted answer). Thanks anyway.
David Hedlund
A: 

hi david,

im having same type of problem. but i didnt understood your post completly.

can u pls explain me a little bit more.

Thanks in advance.

RajaSekhar email:[email protected]

Raja
@Raja: did you read my last answer? (the accepted one). did you try that? i posted all the code I'm using. if you're still having problems after trying that, please specify what you want to do, and what's not working.
David Hedlund