Thursday, 20 September 2012

Android Linkify

What is linkify  ?

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, Phone numbers, Map Locations etc. and making them actionable.

It can be apply on TextView widget and its direct Child views, WebView Widgets. Android provides XML attributes or Java library to apply linkify means containing web url, phone no, email id, map location automatically highlighted and make actionable. Its actions are automatically handled by framework.



 















How to apply Linkify on a TextView?

Create new android project add all like below:

project/res/values/strings.xml
----------------------------------------------------------


    LinkifyTest
     Android 4.1 Most popular Mobile OS. Web: http://android.com, email : androidsupport@gmail.com an Phone on : 01108888888 . Contact for more Information.
    Settings
    MainActivity




project/res/layout/activity_main.xml
----------------------------------------------------------


    



project/src/<packagename>/MainActivity.java
---------------------------------------------------------------------------
package com.example.linkifytest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}



Here If you click on any link it send a framework generated Intent to android OS. Bellow I am placing details of Intents :

1. If Click on any Phone No. The Intent very much similar to looks like below:

INTENT START {act=android.intent.action.VIEW dat=tel:xxxxxxxxxxx cmp=com.android.contacts/.activities.DialtactsActivity (has extras)}

2. If Click on any email id . The Intent very much similar to looks like below:

INTENT START {act=android.intent.action.VIEW dat=mailto:xxxxxxxxxxxxxx@xxxxx.xxx cmp=android/com.android.internal.app.ResolverActivity (has extras)}

3. If Click on any web url . The Intent very much similar to looks like below:

INTENT START {act=android.intent.action.VIEW dat=http://android.com cmp=android/com.android.internal.app.ResolverActivity (has extras)}


All intent are sent by framework and consumed by framework, so some time it necessary to handle the likify intent by developer. For example When user click in Phone No it will display some Options instead of call. Suppose those options are Call, Add to Contact, Send Message, Copy etc... so developers have a questions, Can we handle linkify click event? It is not so easy for my knowledge, there is no such type of framework architecture or interface though developer directly handle it. In this blog I am placing some tricks to handle linkify click event.

 

 

 

How to handle Linkify click event?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Create a 'LinkifyActionChooser' Activity to handle linkify event intents and change as following code.

AndroidManifest.xml
----------------------------------


    

    
        
            
                

                
            
        
        
            
                
                
                
                
                
                
            
            
        
    


res/values/string.xml
----------------------------------
;


    LinkifyTest
     Android 4.1 Most popular Mobile OS. Web: http://android.com, email : androidsupport@gmail.com an Phone on : 01108888888 . Contact for more Information.
    Settings
    MainActivity
    Call
    Add to contact
    Send Message
    Send Mail
    Open
    Cancel




res/layout/main_activity.xml
------------------------------------------------------------


    



res/drawable/dialog_background.xml
------------------------------------------------------------


    


MainActivity.java
------------------------------
package com.example.linkifytest;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Patterns;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView mTextView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView)findViewById(R.id.text1);
        
    }
    

    @Override
 protected void onResume() {
     String bodyString = getString(R.string.hello_world);
     SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
     spannableStringBuilder.append(bodyString);
     
     Linkify.addLinks(spannableStringBuilder, Patterns.PHONE, "myphone:");
     Linkify.addLinks(spannableStringBuilder, Patterns.EMAIL_ADDRESS, "myemail:");
     Linkify.addLinks(spannableStringBuilder, Patterns.WEB_URL, "myweburl:");
     
     mTextView.setText(spannableStringBuilder);
     mTextView.setMovementMethod(LinkMovementMethod.getInstance());
     
  super.onResume();
 }

}

LinkifyActionChooser.java
-------------------------------------------
package com.example.linkifytest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;

public class LinkifyActionChooser extends Activity  {

 private static final String SCHEME_PHONE = "myphone";
 private static final String SCHEME_EMAIL = "myemail";
 private static final String SCHEME_WEBURL = "myweburl";
 

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  getWindow().setBackgroundDrawableResource(R.drawable.dialog_background);
  
  final CharSequence[] actionPhone = { getString(R.string.call),
    getString(R.string.send_message),
    getString(R.string.add_contact) };
  
  final CharSequence[] actionEMail = { getString(R.string.send_mail),
    getString(R.string.send_message),
    getString(R.string.add_contact) };
  
  final CharSequence[] actionWebUrl = { getString(R.string.open),
    getString(R.string.add_contact) };
  
  AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
  

  alt_bld.setTitle(this.getIntent().getDataString()
    .replace(this.getIntent().getScheme() + ":", "").trim());
  
  alt_bld.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
   
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    finish();
   }
  });
  
  if(SCHEME_PHONE.equals(this.getIntent().getScheme())){
   alt_bld.setItems(actionPhone, mPhoneOnclickListener);
  }else if(SCHEME_EMAIL.equals(this.getIntent().getScheme())){
   alt_bld.setItems(actionEMail, mMailOnclickListener);
  }else if(SCHEME_WEBURL.equals(this.getIntent().getScheme())){
   alt_bld.setItems(actionWebUrl, mWebUrlOnclickListener);
  }
  
  AlertDialog alertDialog = alt_bld.create();
  alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
   
   public void onCancel(DialogInterface dialog) {
    // TODO Auto-generated method stub
    finish();
   }
  });
  
  alertDialog.show();
 }
 
 
 
 @Override
 public void onBackPressed() {
  finish();
  super.onBackPressed();
 }



 private OnClickListener mPhoneOnclickListener = new DialogInterface.OnClickListener() {
  
  public void onClick(DialogInterface dialog, int which) {
   //Handle Accordingly Selected Item
   switch(which){
   case 1:
    Log.v("PHONE://xxxxxxxxxxx", "CALL Clicked");
    break;
   case 2:
    Log.v("PHONE://xxxxxxxxxxx", "SEND MESSAGE Clicked");
    break;
   case 3:
    Log.v("PHONE://xxxxxxxxxxx", "ADD TO CONTACT Clicked");
    break;
   }
   finish();
  }
 } ;
 
 private OnClickListener mMailOnclickListener = new DialogInterface.OnClickListener() {
  
  public void onClick(DialogInterface dialog, int which) {
   //Handle Accordingly Selected Item
   switch(which){
   case 1:
    Log.v("MAIL://xxxxxxxxxxx@xxxxx.xxx", "SEND MAIL Clicked");
    break;
   case 2:
    Log.v("MAIL://xxxxxxxxxxx@xxxxx.xxx", "SEND MESSAGE Clicked");
    break;
   case 3:
    Log.v("MAIL://xxxxxxxxxxx@xxxxx.xxx", "ADD TO CONTACT Clicked");
    break;
   }
   finish();
  }
 } ;
 
 private OnClickListener mWebUrlOnclickListener= new DialogInterface.OnClickListener() {
  
  public void onClick(DialogInterface dialog, int which) {
   //Handle Accordingly Selected Item
   switch(which){
   case 1:
    Log.v("WEB://xxxxxxxxxxx.xxx", "OPEN Clicked");
    break;
   case 2:
    Log.v("WEB://xxxxxxxxxxx.xxx", "ADD TO CONTACT Clicked");
    break;
   }
   finish();
  }
 } ;
 
}

1 comment: