How to Add Hyperlink to TextView in Android?
Last Updated :
06 Jun, 2021
Hello geeks, today we are going to make an application where we will see that how we can add a link to a TextView in Android, and using this concept we will add portals - home and practice (of GeeksforGeeks) in our application. So that, user can directly go to these portals from our application.
What we are going to build in this article?
We will be using the strings.xml file to do our task and then will us setMovementMethod() in our java file so that whatever link we provided we can direct there. Here is how our application will look like.
So, now let us see step by step implementation of the application.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working on strings.xml file
Open values > strings.xml file. We have to make two strings to add a hyperlink, so use the code provided below.
XML
<resources>
<string name="app_name">Hyperlink_textview</string>
<!-- href tag is used to add link-->
<!-- link for home text view-->
<string name="hyperlink"><a href="https://www.geeksforgeeks.org/">geeksforgeeks.org</a></string>
<!-- link for practice textview-->
<string name="hyperlink2"><a href="https://practice.geeksforgeeks.org/">practice.geeksforgeeks.org</a></string>
</resources>
Step 3: Working with the activity_main.xml file
In the activity_main.xml file, we have to name the TextViews by a string that we created in the strings.xml file. Below is the code for it.
XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Constraint layout is used to constrain all components easily-->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6C6B74"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView11"
android:layout_width="381dp"
android:layout_height="78dp"
android:layout_marginTop="252dp"
android:background="#1B0D0D"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<!-- ImageView for the logo-->
<ImageView
android:id="@+id/imageView"
android:layout_width="135dp"
android:layout_height="136dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.026"
app:srcCompat="@drawable/gfg_round" />
<!-- TextView for the tagline of geeksforgeeks -->
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="141dp"
android:text="A Computer Science Portal for geeks"
android:textColor="#000C01"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.576"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintVertical_bias="0.022" />
<!-- textview as a container to other two text views-->
<TextView
android:id="@+id/textView4"
android:layout_width="381dp"
android:layout_height="78dp"
android:layout_marginTop="76dp"
android:background="#1B0D0D"
app:layout_constraintBottom_toTopOf="@+id/textView11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.235" />
<!-- Home text view-->
<TextView
android:id="@+id/textView7"
android:layout_width="96dp"
android:layout_height="44dp"
android:gravity="center"
android:text="Home"
android:textColor="#027507"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="@+id/textView4"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toTopOf="@+id/textView4"
app:layout_constraintVertical_bias="0.47" />
<!-- Hyperlink number 1 to the textview-->
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hyperlink"
app:layout_constraintBottom_toBottomOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="@+id/textView4"
app:layout_constraintHorizontal_bias="0.709"
app:layout_constraintStart_toEndOf="@+id/textView7"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.788" />
<!-- Hyperlink number 2 to the textview-->
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Practice"
android:textColor="#027507"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView11"
app:layout_constraintEnd_toEndOf="@+id/textView11"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintStart_toStartOf="@+id/textView11"
app:layout_constraintTop_toTopOf="@+id/textView11"
app:layout_constraintVertical_bias="0.474" />
<!-- Practice text view-->
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hyperlink2"
app:layout_constraintBottom_toBottomOf="@+id/textView11"
app:layout_constraintEnd_toEndOf="@+id/textView11"
app:layout_constraintHorizontal_bias="0.812"
app:layout_constraintStart_toEndOf="@+id/textView12"
app:layout_constraintTop_toTopOf="@+id/textView11"
app:layout_constraintVertical_bias="0.593" />
</androidx.constraintlayout.widget.ConstraintLayout>
After implementing the above code our UI looks like this.
Step 4: Working with the MainActivity.java file
We will be using setMovementMethod() to redirect users to the provided link. We will also be using setcolorlink() method to change the color of the link according to our convenience. Below is the code for it.
Java
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Text view number 1 to add hyperlink
TextView linkTextView = findViewById(R.id.textView8);
// method to redirect to provided link
linkTextView.setMovementMethod(LinkMovementMethod.getInstance());
// method to change color of link
linkTextView.setLinkTextColor(Color.YELLOW);
// Text view number 2 to add hyperlink
TextView linkTextView2 = findViewById(R.id.textView13);
// method to redirect to provided link
linkTextView2.setMovementMethod(LinkMovementMethod.getInstance());
// method to change color of link
linkTextView2.setLinkTextColor(Color.YELLOW);
}
}
Congratulations, you have successfully made a complete application its time to run and test it. You can use this method to redirect users of your application to your website. Here is the final output of your application.
Output:
Similar Reads
Zoom In and Out to TextView in Android
In this article, we are going to implement the zoom-in and zoom-out feature in TextView. As we zoom we will see the image after zooming in. Basically, we are going to learn how to increase or decrease the text size in Android. A sample GIF is given below to get an idea about what we are going to do
2 min read
How to Add SlantedTextView in Android App?
SlantedTextView is an Android library that allows us to easily create a slanted text in an android app. We can use this feature in many apps such as educational courses app or subscription-based app and in many other apps where some features are available for free and some are paid features. Note th
3 min read
How to Add a TextView with Rounded Corner in Android?
TextView is an essential object of an Android application. It comes in the list of some basic objects of android and used to print text on the screen. In order to create better apps, we should learn that how can we create a TextView which have a background with rounded corners. We can implement this
2 min read
How to Add Text Drawable to ImageView in Android?
In many android apps, you will get to see a feature in which you can see a simple text is displayed inside an ImageView or you can get to see that a text is displayed in a specific image or a shape. Mostly this type of view is seen in the Contacts application which is present on your Android device.
5 min read
How to Underline Text in TextView in Android?
In Android, a TextView is a primary UI element used to display text present in the form of characters, numbers, strings, and paragraphs. However, styles cannot be applied to text present in the TextView. So in this article, we will show you how you could underline text in a TextView in Android. Foll
2 min read
How to Add Footer to ListView in Android with Kotlin?
In Android, a ListView is a view used to display a list of items separated by a stroke. A ListView adapter is used to supply items from the main code to the ListView in real-time. A Footer is anything that gets added at the bottom of any element. So, a Footer in ListView is a layout that is present
3 min read
How to add fading TextView animation in Android
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot. A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface
1 min read
How to Display HTML in Textview along with Images in Android?
There are some situations when one needs to display rich formatted text in the app, like for a blog app or an app like Quora but Android's inbuilt function doesn't allow to display inline images by default, moreover they show the ugly blue line to the left of anything inside blockquote tag. Here is
11 min read
How to Implement TextWatcher in Android?
If an application contains a login form to be filled by the user, the login button should be disabled (meaning: it shouldn't be clickable). When the user enters the credentials of the form the button should be enabled to click for the user. So in this article, we are implementing a Text Watcher to t
3 min read
How to Implement TextSwitcher in Android?
In Android, TextSwitcher is a useful tool for displaying text with animations. It can be seen as a special version of ViewSwitcher, where its children are only TextView elements. TextSwitcher allows us to add smooth transitions to text, making it part of the transition widget family. Whenever you ca
3 min read