Sunday, March 30, 2014

Set a link into TextView In Android

Description:
This example will let you set a link in textview will will switch to your phone’s browser and will open the link specified in browser. Also you will be able to dial a number by clicking a link set in the view.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it TextViewLinkDemo.
2.) Write following code into your main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/text1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:autoLink="all"
           android:text="@string/link_text_auto"
           />
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/text3"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/text4"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
</LinearLayout>
3.) Write following code into strings.xml
<resources>
    <string name="hello">Hello World, TextViewLinkDemoActivity!</string>
    <string name="app_name">TextViewLinkDemo</string>
 
    <string name="link_text_auto"><b>text1:</b> This is some text.  In
      this text are some things that are actionable.  For instance,
      you can click on http://www.gmail.com and it will launch the
      web browser.  You can click on google.com too.  And, if you
      click on (415) 555-1212 it should dial the phone.
    </string>
</resources>
4.) Build and run your code and check the output given below in the doc.
Steps:
1.) Create a project named TextViewLinkDemo and set the information as stated in the image.
Build Target: Android 2.3
Application Name: TextViewLinkDemo
Package Name: com.org. TextViewLinkDemo
Activity Name: TextViewLinkDemo
Min SDK Version: 9
2.) Open TextViewLinkDemo.java file and write following code there:
package com.org.TextViewLinkDemo;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
public class TextViewLinkDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView t3 = (TextView) findViewById(R.id.text3);
        t3.setText(
            Html.fromHtml(
                "<b>text3:</b>  Text with a " +
                "<a href="http://www.gmail.com">link</a> " +
                "created in the Java source code using HTML."));
        t3.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString ss = new SpannableString(
            "text4: Click here to dial the phone.");
        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
                   Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
                   Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        TextView t4 = (TextView) findViewById(R.id.text4);
        t4.setText(ss);
        t4.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
3.) Compile and build the project.
4.) Run on simulator and click onto the links shown in the view. It will open browser and browse the link you have clicked in text view.
Output

Related Posts by Categories

0 comments:

Post a Comment