TextView में क्लिक करने योग्य लिंक बनाना
यह उदाहरण दर्शाता है कि TextView में HTML लिंक को क्लिक करने योग्य कैसे बनाया जाए, LinkMovementMethod और autoLink का उपयोग करके।
चरण 1: नया प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में File → New Project चुनें।
चरण 2: लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold"
android:layout_marginBottom="32dp" />
<TextView
android:id="@+id/textViewLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/message_with_link"
android:textSize="20sp"
android:textStyle="bold"
android:autoLink="web|email|phone|map"
android:linksClickable="true"
android:textColorLink="@android:color/holo_blue_dark"
android:textColorHighlight="@android:color/holo_blue_light" />
<TextView
android:id="@+id/textViewCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:gravity="center"
android:textSize="18sp"
android:lineSpacingMultiplier="1.5" />
</LinearLayout>
चरण 3: स्ट्रिंग संसाधन (res/values/strings.xml)
<resources>
<string name="app_name">Clickable Links Demo</string>
<string name="message_with_link">
<a href="https://www.google.com/">गूगल खोलने के लिए यहां टैप करें</a>
\n\n
<a href="mailto:support@example.com">हमें ईमेल करें</a>
\n\n
<a href="tel:+911234567890">हमें कॉल करें</a>
\n\n
<a href="geo:28.6139,77.2090">नई दिल्ली मानचित्र पर देखें</a>
</string>
</resources>
चरण 4: MainActivity.kt
import android.os.Bundle
import android.text.Html
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var textViewLink: TextView
private lateinit var textViewCustom: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textViewLink = findViewById(R.id.textViewLink)
textViewCustom = findViewById(R.id.textViewCustom)
// विधि 1: XML autoLink + LinkMovementMethod (strings.xml के लिए)
textViewLink.movementMethod = LinkMovementMethod.getInstance()
// विधि 2: प्रोग्रामेटिकली HTML से सेट करें
setHtmlText()
// विधि 3: कस्टम ClickableSpan (पूर्ण नियंत्रण)
setCustomClickableText()
}
private fun setHtmlText() {
val htmlText = """
<p>हमारी वेबसाइट पर जाएं: <a href="https://www.tutorialspoint.com/">Tutorials Point</a></p>
<p>हमसे संपर्क करें: <a href="mailto:contact@tutorialspoint.com">ईमेल</a></p>
<p>हमें कॉल करें: <a href="tel:+1234567890">+1-234-567-890</a></p>
""".trimIndent()
// HTML पार्स करें (API 24+ के लिए fromHtml अलग है)
val spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT)
} else {
@Suppress("DEPRECATION")
Html.fromHtml(htmlText)
}
textViewCustom.text = spanned
textViewCustom.movementMethod = LinkMovementMethod.getInstance()
}
private fun setCustomClickableText() {
val text = "उपयोग की शर्तें और गोपनीयता नीति स्वीकार करें"
val spannable = SpannableString(text)
// "उपयोग की शर्तें" पर क्लिक
val termsStart = 0
val termsEnd = "उपयोग की शर्तें".length
spannable.setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
Toast.makeText(this@MainActivity, "उपयोग की शर्तें क्लिक की गई", Toast.LENGTH_SHORT).show()
// openTermsActivity()
}
override fun updateDrawState(ds: android.text.TextPaint) {
super.updateDrawState(ds)
ds.color = resources.getColor(android.R.color.holo_blue_dark)
ds.isUnderlineText = false // अंडरलाइन हटाएं
}
},
termsStart, termsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
// "गोपनीयता नीति" पर क्लिक
val privacyStart = text.lastIndexOf("गोपनीयता नीति")
val privacyEnd = text.length
spannable.setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
Toast.makeText(this@MainActivity, "गोपनीयता नीति क्लिक की गई", Toast.LENGTH_SHORT).show()
// openPrivacyActivity()
}
override fun updateDrawState(ds: android.text.TextPaint) {
super.updateDrawState(ds)
ds.color = resources.getColor(android.R.color.holo_green_dark)
ds.isUnderlineText = false
}
},
privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
textViewCustom.append("\n\n") // पिछले टेक्स्ट के बाद जोड़ें
textViewCustom.append(spannable)
textViewCustom.movementMethod = LinkMovementMethod.getInstance()
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.clickablelinks">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
लिंक प्रकार और URI योजनाएं:
| प्रकार | URI योजना | उदाहरण |
|---|---|---|
| वेब | https:// | https://example.com |
| ईमेल | mailto: | mailto:user@domain.com?subject=Hello |
| फोन | tel: | tel:+911234567890 |
| SMS | sms: | sms:+911234567890?body=Hello |
| मानचित्र | geo: | geo:28.6139,77.2090?q=दिल्ली |
| मार्केट/प्ले स्टोर | market:// | market://details?id=com.example.app |
| इंटेंट | intent:// | intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end |
स्टाइलिंग लिंक (थीम में):
<!-- res/values/themes.xml -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorLink">@color/custom_link_color</item>
<item name="textAppearanceBody1">@style/CustomBodyText</item>
</style>
<style name="CustomBodyText" parent="TextAppearance.MaterialComponents.Body1">
<item name="android:textColorLink">@color/custom_link_color</item>
<item name="android:textColorHighlight">@color/custom_link_highlight</item>
</style>
सामान्य समस्याएं और समाधान:
- लिंक क्लिक नहीं होता: `movementMethod = LinkMovementMethod.getInstance()` सेट करना न भूलें
- HTML टैग नहीं दिखते: `Html.fromHtml()` का उपयोग करें, सीधे स्ट्रिंग सेट न करें
- रंग नहीं बदलता: `textColorLink` XML या थीम में सेट करें
- अंडरलाइन हटाएं: `ClickableSpan` में `ds.isUnderlineText = false` सेट करें