Friday, April 15, 2011

Custom text linkify in android

Here is the demo for custom text linkify in android.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.text.util.Linkify.TransformFilter;
import android.widget.TextView;

public class LinkifyTextDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView textView = (TextView)findViewById(R.id.txtSampleTextView);
textView.setText("This is a demo of custom text linkify, developed by @Bharat");

MatchFilter matchFilter = new MatchFilter() {
public final boolean acceptMatch(CharSequence s, int start, int end) {
// you can compare match over here
// return s.toString().equals("@Bharat");
return true;
}
};
TransformFilter transformFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return "www.android-geek.blogspot.com/2011/04/linkify-text-in-android.html";
}
};

Pattern pattern = Pattern.compile("@Bharat");
String scheme = "http://";
Linkify.addLinks(textView, pattern, scheme, matchFilter, transformFilter);
}
}

screenshoot:



4 comments:

  1. Hi ,

    My requirement is to stop loaing link and invoke an activity.
    Any ideas on that?

    ReplyDelete
  2. Your blog creativity is really good.This is one of the good quality post.Nice to read about your information.Keep it up.
    Android app developers

    ReplyDelete
  3. This one article alone shows off your excellent writing skills. I am very impressed with your views and interesting content. I hope you intend to continue writing more informational articles.

    Android developers

    ReplyDelete