Tuesday, February 12, 2013

Android Use of Linkify

The Android framework provides an easy way to automatically convert text patterns into clickable links. By default, Android knows how to recognize web URLs, email addresses, map addresses, and phone numbers, but it also includes a flexible mechanism for recognizing and converting additional text patterns, as well.

This article covers the use of Linkify to turn ordinary text views into richer link-oriented content that causes Android intents to fire when a link is selected.  

Linkify: The Linkify class in the SDK is perfect for creating a wiki note pad. This class lets you specify a regular expression to match, and a scheme to prepend. The scheme is a string that, when the matched text is added, forms a Content URI to allow the correct data to be looked up.

create a project with the name AndroidHyperLinkButton and have a main activity with the following code

MainActivity.java


package com.example.androidhyperlinkbutton;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

String linkdata = "hi how are  yo dear? http://upadhyayjiteshandroid.blogspot.in/ ,it is great to see you at my blog thank you!! my contact is 7829955125";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView noteView = (TextView) findViewById(R.id.infoTxtCredits);
noteView.setText(linkdata);
Linkify.addLinks(noteView, Linkify.ALL);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


res/layout/activity_main.xml

<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=".MainActivity" >

    <TextView
         android:id="@+id/infoTxtCredits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>


2 comments:

  1. I am glad to know that you are feeling to get this code example great. please spread and share knowledge as much as possible.

    ReplyDelete