Here I am going to explain a very simple method to implementing hyperlinks to any TextView in android studio.
For better understanding watch the video below (In HD+ Quality) for step by step guidance.
Steps involved in video also described below:
There are following 3 steps involved to Create a Static Clickable Link Inside the TextView.
1- Adding TextView in Activity
2- Adding String in string.xml
3- Implementing code in MainActivity.java
1- Adding Some TextView
First of all we need to add TextView into Main-activity.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text1"
android:id="@+id/weblink"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
/>
|
2- Adding A New String into String.xml
Now Go to res à values à string on left side menu and create a new string as like this string in strings.xml:
<string name="infopalacesslink"><a href="http://www.infopalacess.com">Go To My Website</a></string>
3- Implementing code in MainActivity.java
Now Go to “MainActivity.java” and write this code:
TextView text = (TextView) findViewById(R.id.textview);
text.setMovementMethod(LinkMovementMethod.getInstance());
|
For more detail please refer to Picture below:
You are done! You should get a TextView like the one in the following screenshot and when you tap on the hyperlink the browser should open! Cool!
Note! you want to open a static link without doing other operations? So just follow the first part of this tutorial! Otherwise, if you want to have more control on the link customization and operations after the user taps on it, follow the second part of the tutorial which will come later.
0 Comments