Android TextView के विशिष्ट भाग को क्लिकेबल बनाना
यह उदाहरण दर्शाता है कि SpannableString और ClickableSpan का उपयोग करके TextView के केवल विशिष्ट शब्दों या वाक्यांशों को क्लिकेबल कैसे बनाया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I want THIS and THIS to be CLICKED!"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold|italic"/>
</RelativeLayout>
चरण 3: MainActivity में ClickableSpan लागू करें (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
String text = "I want THIS and THIS to be CLICKED";
SpannableString spannableString = new SpannableString(text);
// पहला "THIS" (इंडेक्स 7-11)
ClickableSpan clickableSpan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "पहला THIS क्लिक हुआ", Toast.LENGTH_SHORT).show();
}
};
// दूसरा "THIS" (इंडेक्स 16-20)
ClickableSpan clickableSpan2 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "दूसरा THIS क्लिक हुआ", Toast.LENGTH_SHORT).show();
}
};
// "CLICKED" शब्द (इंडेक्स 27-34)
ClickableSpan clickableSpan3 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "CLICKED क्लिक हुआ", Toast.LENGTH_SHORT).show();
}
};
// स्पैन सेट करें
spannableString.setSpan(clickableSpan1, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(clickableSpan2, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(clickableSpan3, 27, 34, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// TextView पर लागू करें
textView.setText(spannableString);
// क्लिक इवेंट काम करने के लिए MovementMethod सेट करना आवश्यक है
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
मुख्य बिंदु
- SpannableString: टेक्स्ट के विशिष्ट भागों पर स्टाइल/व्यवहार लागू करने के लिए
- ClickableSpan: क्लिक इवेंट हैंडल करने के लिए
- LinkMovementMethod: क्लिक इवेंट सक्षम करने के लिए अनिवार्य
- SPAN_EXCLUSIVE_EXCLUSIVE: स्पैन सीमाओं को परिभाषित करता है
ऐप चलाएं
डिवाइस पर ऐप चलाएं और हाइलाइटेड शब्दों पर टैप करके अलग-अलग टोस्ट संदेश देखें।


