18 July 2014

Giving a link of a website in your Application

Here we will learn how to visit a website from your android application .

What do you need ?
  • A Layout
  • A TextView
  • A java file
  • Name of the website you desire to link to
What are we going to do ?
-> We will make a TextView with the name of the website which will be linked to the website through the browser .

Step 1 : Add a TextView (or two) with the name of your Website Or a catchy link tag. (activity_main.xml)
in res->layout folder .
textview
 Code for activity_main.xml is given below :
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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Click the link to visit my website"
        android:textAppearance="?android:attr/textAppearanceLarge" />    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Link to Made In Android"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Step 2 : Now we will make the logic to make the TextView as a link .Go to MainActivity,java in src folder of your project .
MainActivity.java
package com.mia.websitelink;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

      public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
              
             setContentView(R.layout.activity_main);
             TextView webs =(TextView)findViewById(R.id.textView2);
             webs.setTextColor(Color.BLUE);
            
             SpannableString content = new SpannableString("Link to Made In Android");
             content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            
                webs.setText(content);
                           
                webs.setOnClickListener(new View.OnClickListener() {                    
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://madeinandroid.blogspot.in/"));
                        startActivity(i);            
                    }
                });
      }
}
You can change the address(shown as green colored in the code to address you wish to visit)

Step 3 : Run the project .

Explanation of the above code :
  • We usually write our code we want to run presently in the app in onCreate() method . Then we pass the id of the layout in setContentView() method .
public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
              
             setContentView(R.layout.activity_main);
  • We make a variable webs of type TextView which we relate it to the TextView we want our link to be (textView2)
TextView webs =(TextView)findViewById(R.id.textView2);
  • We set the color of the text to BLUE in the line of code given below .
webs.setTextColor(Color.BLUE);
SpannableString content = new SpannableString("Link to Made In Android");

content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

webs.setText(content);
  • We then define the onClickListener() method which performs the actions when we click the text .
webs.setOnClickListener(new View.OnClickListener() {                    
                    @Override
                    public void onClick(View arg0) {
  • Then we define the ACTION_VIEW parameter of Intent class to display the browser as we click the link .Then we use Uri.parse which takes input as the link of our website and also validates it . Then we finally start our Activity using startActivity() method .
Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://madeinandroid.blogspot.in/"));
                        startActivity(i);            
                    }
                });
Stay Tuned with Made In Android

Previous Page Next Page Home
Top