Advanced Android - Linkify Class



Linkify is a helper class that creates hyperlinks within Text View classes through RegExpattern matching.

Example

This example demostrate about how to integrate Linkify Class.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to 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"
   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>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

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

import java.util.regex.Pattern;

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 : send18.com");
      Linkify.addLinks(WebSiteone, Linkify.WEB_URLS);
      
      TextView WebSitetwo = (TextView) findViewById(R.id.textview2);
      WebSitetwo.setText("Go to : Tutorialspoint.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=");
   }
}

Step 4 − Add the following code to manifest.xml

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

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Link
Advertisements