Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android TextView में क्लिक करने योग्य लिंक कैसे बनाएं?

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 ब्राउज़र में खुलेगा।

Android TextView में क्लिक करने योग्य लिंक कैसे बनाएं?
  1. Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें? Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें?

    Android में ButterKnife से व्यू बाइंडिंग सरल बनाना ButterKnife एक व्यू बाइंडिंग लाइब्रेरी है जो findViewById() कॉल्स को एनोटेशन से बदल देती है। नोट: ButterKnife अब डिप्रिकेटेड है; आधुनिक ऐप्स के लिए View Binding (बिल्ट-इन) या Data Binding उपयोग करें। ⚠ डिप्रिकेटेड: Jake Wharton ने ButterKnife को म

  1. Android में JSON कैसे पार्स करें? Android में JSON कैसे पार्स करें?

    Android में JSONObject से सरल JSON पार्स करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे एक JSON स्ट्रिंग से JSONObject बनाकर नेस्टेड ऑब्जेक्ट्स से मान निकाले जाएं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट फ़ाइल में कोड जोड़ें res/layout

  1. Android TextView में टेक्स्ट को जस्टिफाई कैसे करें? Android TextView में टेक्स्ट को जस्टिफाई कैसे करें?

    TextView में टेक्स्ट जस्टिफिकेशन Android 8.0 (API 26) से android:justificationMode=inter_word एट्रिब्यूट का उपयोग करके TextView में टेक्स्ट को जस्टिफाई (दोनों किनारों पर संरेखित) किया जा सकता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें (न्यूनतम API 26 या अधिक)। चरण 2