Tutorial details

Android Custom Hyperlinked TextView | App Code for Sale | Preview

Android Custom Hyperlinked TextView | Android Tutorial

How to use Custom Hyperlinked TextView

Overview PAGE TOP

Finding Links in Android is very simple you might have heard of Linkify which is a great class in which there are many static methods for getting a simple job done but the problem is that in Linkify you cannot specify the behavior that you require on clicking the links at the Most what Linkify does is that it simply finds the links based on the “Pattern” you provide and adds a “Scheme” string to complete the URL and created a Android Intent for Launching the Browser. The Linkify behavior cannot be overridden as all the methods in Linkify class are static final.

This was the issue that inspired me to create a Custom TextView Widget the one which collects the links in my scenario I also programmed it to collect the the Strings starting with “@” and “#” but this things can be changed by simply changing the Patterns that you require and giving the proper Regular Expression for them.

The LinkEnableTextView class is like this:

001 import java.util.ArrayList;
002 import java.util.regex.Matcher;
003 import java.util.regex.Pattern;
004 import android.content.Context;
005 import android.text.Spannable;
006 import android.text.SpannableString;
007 import android.text.style.ClickableSpan;
008 import android.util.AttributeSet;
009 import android.view.View;
010 import android.widget.TextView;
011 
012 
013 public class LinkEnabledTextView  extends TextView
014 {
015 // The String Containing the Text that we have to gather links from private SpannableString linkableText;
016 // Populating and gathering all the links that are present in the Text
017 private ArrayList<Hyperlink> listOfLinks;
018 
019 // A Listener Class for generally sending the Clicks to the one which requires it
020 TextLinkClickListener mListener;
021 
022 // Pattern for gathering @usernames from the Text
023 Pattern screenNamePattern = Pattern.compile('(@[a-zA-Z0-9_]+)');
024 
025 // Pattern for gathering #hasttags from the Text
026 Pattern hashTagsPattern = Pattern.compile('(#[a-zA-Z0-9_-]+)');
027 
028 // Pattern for gathering http:// links from the Text
029 Pattern hyperLinksPattern = Pattern.compile('([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\'>\\]\\)]*[^\\. ,'\'>\\]\\)])');
030 
031 public LinkEnabledTextView(Context context, AttributeSet attrs)
032 {
033     super(context, attrs);
034     listOfLinks = new ArrayList<Hyperlink>();
035 
036 }
037 
038 public void gatherLinksForText(String text)
039 {
040     linkableText = new SpannableString(text);
041  //gatherLinks basically collects the Links depending upon the Pattern that we supply
042  //and add the links to the ArrayList of the links
043    
044     gatherLinks(listOfLinks, linkableText, screenNamePattern);
045     gatherLinks(listOfLinks, linkableText, hashTagsPattern);
046     gatherLinks(listOfLinks, linkableText, hyperLinksPattern);
047 
048     for(int i = 0; i< listOfLinks.size(); i++)
049     {
050         Hyperlink linkSpec = listOfLinks.get(i);
051         android.util.Log.v('listOfLinks :: ' + linkSpec.textSpan, 'listOfLinks :: ' + linkSpec.textSpan);
052          
053         // this process here makes the Clickable Links from the text
054           
055         linkableText.setSpan(linkSpec.span, linkSpec.start, linkSpec.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
056     }
057 
058      
059      // sets the text for the TextView with enabled links
060       
061     setText(linkableText);
062 }
063 
064 
065  // sets the Listener for later click propagation purpose
066   
067 public void setOnTextLinkClickListener(TextLinkClickListener newListener)
068 {
069     mListener = newListener;
070 }
071 
072   //The Method mainly performs the Regex Comparison for the Pattern and adds them to
073   //listOfLinks array list
074   
075 
076 private final void gatherLinks(ArrayList<Hyperlink> links,
077                                Spannable s, Pattern pattern)
078 {
079     // Matcher matching the pattern
080     Matcher m = pattern.matcher(s);
081 
082     while (m.find())
083     {
084         int start = m.start();
085         int end = m.end();
086 
087          
088     // Hyperlink is basically used like a structure for storing the information about
089     // where the link was found.
090          
091         Hyperlink spec = new Hyperlink();
092 
093         spec.textSpan = s.subSequence(start, end);
094         spec.span = new InternalURLSpan(spec.textSpan.toString());
095         spec.start = start;
096         spec.end = end;
097 
098         links.add(spec);
099     }
100 }
101 
102 
103 // This is class which gives us the clicks on the links which we then can use.
104   
105 
106 public class InternalURLSpan extends ClickableSpan
107 {
108     private String clickedSpan;
109 
110     public InternalURLSpan (String clickedString)
111     {
112         clickedSpan = clickedString;
113     }
114 
115     @Override
116     public void onClick(View textView)
117     {
118         mListener.onTextLinkClick(textView, clickedSpan);
119     }
120 }
121 
122 
123 // Class for storing the information about the Link Location
124 
125 
126 class Hyperlink
127 {
128     CharSequence textSpan;
129     InternalURLSpan span;
130     int start;
131     int end;
132 }

Now, having this you require just another interface for propagating the clicks to the place you require to handle them in my case I implemented the interface in my Activity and simple wrote a Log Command there.

The TextLinkClickListener interface is like this:

01  import android.view.View;
02  
03  public interface TextLinkClickListener
04  {
05  
06    
07    //  This method is called when the TextLink is clicked from LinkEnabledTextView
08     
09  public void onTextLinkClick(View textView, String clickedString)
10  }

After doing all this you just require to create an Activity using the Custom LinkEnabledTextView and check the things out yourself. There are a few things that you must do while creating a object of the Custom LinkEnabledTextView those are mentioned and described in the Code of the Activity below:

01  import android.text.method.MovementMethod;
02  import com.umundoinc.Tvider.Component.LinkEnabledTextView.LinkEnabledTextView;
03  import com.umundoinc.Tvider.Component.LinkEnabledTextView.TextLinkClickListener;
04  import android.app.Activity;
05  import android.graphics.Color;
06  import android.os.Bundle;
07  import android.text.method.LinkMovementMethod;
08  import android.view.View;
09  
10  //Here the Activity is implementing the TextLinkClickListener the one we have created
11  //the Clicks over the Links are forwarded over here from the LinkEnabledTextView
12    
13  public class TextViewActivity  extends Activity  implements TextLinkClickListener
14  {
15  private LinkEnabledTextView check;
16  protected void onCreate(Bundle savedInstance)
17  {
18      super.onCreate(savedInstance);
19  
20      String text  =  "This is a #test of regular expressions with http://example.com/ links as used in @twitter for performing various operations based on the links this handles multiple links likehttp://this_is_fun.com/ and #Awesomess and @Cool";
21  
22      check = new LinkEnabledTextView(this, null);
23      check.setOnTextLinkClickListener(this);
24      check.gatherLinksForText(text);
25      check.setTextColor(Color.WHITE);
26      check.setLinkTextColor(Color.GREEN);
27  
28      MovementMethod m = check.getMovementMethod();
29      if ((m == null) || !(m instanceof LinkMovementMethod)) {
30          if (check.getLinksClickable()) {
31              check.setMovementMethod(LinkMovementMethod.getInstance());
32          }
33      }
34  
35      setContentView(check);
36  }
37  
38  public void onTextLinkClick(View textView, String clickedString)
39  {
40      android.util.Log.v('Hyperlink clicked is :: ' + clickedString, 'Hyperlink clicked is :: ' + clickedString);
41  }

Here, is the Screenshot describing the output in my Listener method I programmed it to display a Toast but anything can be achieved in same method.

noe.png

Now that’s pretty much all that you’ll need that makes the Custom LinkEnabledTextView working, try it out and share your views and reviews over it.

Reference PAGE TOP

http://www.javacodegeeks.com/2012/09/android-custom-hyperlinked-textview.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.