Android TextView में HTML लिंक को क्लिक करने योग्य बनाना
strings.xml में HTML एंकर टैग का उपयोग करके TextView में क्लिक करने योग्य हाइपरलिंक बनाने का सरल तरीका।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project।
चरण 2: लेआउट में TextView
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:text="@string/messageWithLink" />
</RelativeLayout>
चरण 3: strings.xml में HTML लिंक परिभाषित करें
res/values/strings.xml:
<resources>
<string name="app_name">Sample</string>
<string name="messageWithLink"><a href="https://www.google.com/">Tap here to open Link</a></string>
</resources>
चरण 4: MainActivity.java में LinkMovementMethod सेट करें
package app.com.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textViewLink);
// यह लाइन लिंक को क्लिक करने योग्य बनाती है
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
चरण 5: AndroidManifest.xml
मानक मैनिफेस्ट। इंटरनेट परमिशन की आवश्यकता नहीं क्योंकि सिस्टम ब्राउज़र खुलता है।
ऐप चलाएं
"Tap here to open Link" टेक्स्ट पर क्लिक करने पर Google ब्राउज़र में खुलेगा।