Monday, 16 September 2013

Using Linkify

Auto Link Text Links With Text Vie

Auto Link Text Links With Text Vie

Create new Android Project
Project Name: Auto Link Textview
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Auto Link Textview
Package Name: com.shaikhhamadali.blogspot.autolinktextview
Create layout file: activity_auto_link_text_view

  1.create layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AutoLinkTextView" >

    <TextView
        android:id="@+id/TVAutoLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text=""
        android:textSize="30sp" />

</RelativeLayout>

  2. code of activity:

package com.shaikhhamadali.blogspot.autolinktextview;

import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class AutoLinkTextView extends Activity {
 TextView TVAutoLink;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_auto_link_text_view);
  //create Text View Reference
  TVAutoLink = (TextView)findViewById(R.id.TVAutoLink);
  //crate text links
  String textLinks = "" +
    "This is a Demo : How you can use Linkify to create auto-link to your text entry.\n" +
    "\n" +
    "URL: http://shaikhhamadali.blogspot.com/ \n" +
    "Email: shaikhhamadali@gmail.com \n" +
    "Phone: (92)-334-200-1098 \n" +
    "Address: P.E.C.H.S, Karachi, Pakistan \n";
  //check that,is text view null or not.
  if(TVAutoLink != null) {
   //set text to text view
   TVAutoLink.setText(textLinks);
   /*Breaf into of Linkyfy class :Linkify take a piece of text and a regular expression and turns
    *all of the regex matches in the text into clickable links. This is particularly useful for
    *matching things like email addresses, web urls, etc. and making them actionable. Alone with
    *the pattern that is to be matched, a url scheme prefix is also required.
    *Any pattern match that does not begin with the supplied scheme will have the scheme
    *prepended to the matched text when the clickable url is created. For instance, if you
    *are matching web urls you would supply the scheme http://. If the pattern matches example.com,
    *which does not have a url scheme prefix, the supplied scheme will be prepended to create 
    *http://example.com when the clickable url link is created. 
    */
   Linkify.addLinks(TVAutoLink, Linkify.ALL); // linkify all links in text.
   //you can set link color with the help of text view property
   TVAutoLink.setLinkTextColor(Color.GREEN);
  }
 }
}

  3. note that:

  • you can use this on button onclick,on action_down,on the fly etc.
  • detailed info about Linkify
  • you can use this in many ways (dynamic text,user entered text etc).
  • you can change color of links you want with the help of the property of text view  TVAutoLink.setLinkTextColor(Color.GREEN)

  4. conclusion:


  • Some information about how to linkify Class usage.
  • Know how to Linkify all the text links in Text View.

  5. about the post:


  • The code seems to explain itself due to comments, if you have any question you can ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:
        you can download the source code here

Cheers,

Hamad Ali Shaikh