Java Andorid Program to Demonstrate Linkify Class in Android

«
»
This Android Java Program demonstrates hyperlinks within Text View using Linkify class in Andorid.

Here is source code of the Program demonstrates a Simple Compound Control. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Linkify is a helper class that creates hyperlinks within Text View (and Text View-derived) classes through RegEx(or Regular Expressions)pattern matching.
Text that matches a specified RegEx pattern will be converted into a clickable hyperlink that implicitly fires startActivity(new Intent(Intent.ACTION_VIEW, uri)),using the matched text as the target URI.

advertisement

Main Activity

 
package com.example.linkify;
 
import java.util.regex.Pattern;
 
import android.os.Bundle;
import android.app.Activity;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.view.Menu;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        TextView WebSiteone = (TextView) findViewById(R.id.textview1);
        WebSiteone.setText("Go to : google.com");
        Linkify.addLinks(WebSiteone, Linkify.WEB_URLS);
 
        TextView WebSitetwo = (TextView) findViewById(R.id.textview2);
        WebSitetwo.setText("Go to : sanfoundry.com");
        Linkify.addLinks(WebSitetwo, Linkify.WEB_URLS);
 
        TextView myCustomLink = (TextView) findViewById(R.id.textView3);
        Pattern p1 = Pattern.compile("\\bAndroid+\\b");
        myCustomLink.setText("Click on Android " + 
        "to search it on google");
        Linkify.addLinks(myCustomLink, p1, "http://www.google.ie/search?q=");
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

Xml

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Remember to add permission to acess internet by specifying so in the Andorid Manifest.

AndoridManifest.xml

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.linkify"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.linkify.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Activity_Main.xml

Check this: BCA MCQs | Programming MCQs
advertisement
 
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="95dp"
        android:text="hello"
        android:textSize="30dp" />
 
    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="186dp"
        android:text="sanfoundry"
        android:textSize="30dp" />
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textview2"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textview2"
        android:layout_marginTop="88dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

Capture

advertisement

google

san

android

advertisement

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.