html and activity links in TextView

So we need to linkify a TextView.


By linkify, I mean adding links to a TextView. These links could either be html links that link to a webpage, or some url that links to an Activity (launches an activity).




TextView mTextSample = (TextView) findViewById(R.id.yourTextView);
String text = "Visit my blog sherifandroid.blogspot.com or run the myactivity callback";
mTextSample.setText(text);

Pattern pattern = Pattern.compile("sherifandroid.blogspot.com");
Linkify.addLinks(mTextSample, pattern, "http://");
Pattern pattern2 = Pattern.compile("myactivity");
Linkify.addLinks(mTextSample, pattern2, "sherif-activity://");

Now, the first pattern with the html link to my blogger page will work correctly.

However the second part should startActivity myactivity.
It will not work unless we specify that sherif-activity://myactivity is a callback url for an activity of our package.

So, for this to work and call say TestActivity!
Your TestActivity in AndroidManifest.xml should look like this:

<activity
	android:label="@string/app_name"
	android:name=".TestActivity">
	<intent-filter>
		<data android:host="myactivity" android:scheme="sherif-activity" />
		<action android:name="android.intent.action.VIEW" />
		<category android:name="android.intent.category.DEFAULT" />
		<category android:name="android.intent.category.BROWSABLE" />
	</intent-filter>
 </activity>

Congradulations, you just created a link to your website and to your activity in the same TextView.

How about sending some data to the new activity using the link!?

Lets first investigate the other way of linkifying a TextView. This would be using normal html tags, namely the <a> tag.


TextView mTextSample = (TextView) findViewById(R.id.yourTextView);
String text = "Visit my blog <a href=\"http://sherifandroid.blogspot.com/\">mysite</a> or run the <a href=\"sherif-activity://myactivity\">myactivity</a> callback";
mTextSample.setText(Html.fromHtml(text));
mTextSample.setMovementMethod(LinkMovementMethod.getInstance());

This will work perfectly like the previous approach.

Now, let us add some data to the link and ultimately the intent that will open the new activity.
We will use the normal GET parameters to add data.

so will will comment out one line of code which initializes text and change it so some data is added to linked activity.


String text = "Visit my blog <a href=\"http://sherifandroid.blogspot.com/\">mysite</a> or run the <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback";
//String text = "Visit my blog <a href=\"http://sherifandroid.blogspot.com/\">mysite</a> or run the <a href=\"sherif-activity://myactivity\">myactivity</a> callback";

So How can we retrieve these extra parameters in the TestActivity!? In this case, the parameter author and nick
It is rather simple:


String author = getIntent().getData().getQueryParameter("author");
String nick = getIntent().getData().getQueryParameter("nick");


Click here for Sample Project + Source Code (download) 

17 comments:

  1. Thank You So Much..and the update to call activity with parameters made my day..!!

    ReplyDelete
  2. thank Sherif elKhatib! It work perfectly! :)

    ReplyDelete
  3. This is works great.. thank you

    ReplyDelete
  4. Check out for working example using tag

    http://javatechig.com/2013/04/07/how-to-display-html-in-android-view/

    ReplyDelete
  5. Thanks! This is help for me

    ReplyDelete
  6. I found this as an informative and interesting post,

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. However nothing of it will be possible if there were not PSD to HTML slicing possibilities. The construction of great buildings, begin with laying down the very basic singular brick in the foundation.
    StartPoint Hastings

    ReplyDelete
  10. It is extremely affordable to invest in an Android based system. Besides, you can get an experienced Android application developer at cost effective rates. Download game killer

    ReplyDelete
  11. Nice information Regarding htmls and links in text view My sincere thanks for sharing this post and please continue to share this kind of post
    Android Training in Chennai

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    Android Online Training

    ReplyDelete
  14. really nice blog has been shared by you.. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    android training in chennai

    ReplyDelete
  15. Thank you so much for your blog. I have been using it as reference for my students during Android Course in Chennai. It has been so much useful, keep writing more


    Android Training in Chennai

    ReplyDelete
  16. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this. but now i got some knowledge about this. so keep on sharing such kind of an interesting blogs.
    android training in chennai

    ReplyDelete
  17. Its an amazing article that you share about PSD To HTML conversion. Thank you so much.

    ReplyDelete