In Android Studio, you can make parts of a TextView clickable by setting its movement method to LinkMovementMethod and defining clickable spans within the text. Here is an example of how you can do this:
1. Set the movement method of the TextView to LinkMovementMethod:
textView.setMovementMethod(LinkMovementMethod.getInstance());
2. Define the clickable spans within the text by creating a SpannableString:
SpannableString spannableString = new SpannableString("This is a link");
what is SpannableString?
A SpannableString is a subclass of the Java String class that allows you to attach spans to a portion of the text. Spans are used to apply different formatting styles to parts of the text, such as changing the color, font size, or underlining a word. They are particularly useful when working with text views in Android, as they allow you to make parts of the text clickable or to display images within the text.
A SpannableString can be created by passing a regular string to its constructor. You can then use the setSpan method to apply spans to specific parts of the string. The setSpan method takes four arguments: the span to be applied, the start position of the text to be affected, the end position of the text to be affected, and a flag that determines whether the span should be applied to the text before or after the start and end positions.
SpannableString spannableString = new SpannableString("This is a string with a span.");
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(colorSpan, 10, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
The above code creates a new SpannableString, applies a ForegroundColorSpan to the substring “string with” and make the text red.
You can also apply multiple spans to the same text by calling the setSpan method multiple times with different start and end positions and different spans. You can remove specific spans by calling removeSpan on a SpannableString and passing the span you want to remove as an argument.
Finally, once you have your SpannableString set up and formatted as you want it, you can set it as the text of a TextView or any other widget that accepts a CharSequence as text.
3. Use the setSpan method to define the clickable parts of the text, and pass in a ClickableSpan as an argument:
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
// Code to handle the click event
}
}, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
what is ClickableSpan?
A ClickableSpan is a type of span that can be applied to a portion of text in an Android TextView, making it clickable. ClickableSpan is an abstract class, so it can be used as a base class for more specific types of clickable spans, such as URLSpan.
When a user clicks on a text that has a ClickableSpan attached, it will trigger the onClick method of that span. This method is where you can specify the action to be taken when the text is clicked, such as opening a web page or launching an activity.
Here is an example of how to use ClickableSpan:
SpannableString spannableString = new SpannableString("This is a clickable text");
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// Code to handle the click event
}
}, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
The above code create a SpannableString, and makes the first 4 characters “This” clickable, when the user clicks on “This” text it calls the onClick method where you can specify the action you want to take, for example opening a webpage, activity, dialog or anything.
As a best practice, you should also set the TextView to be clickable and also set the movement method to LinkMovementMethod to be able to respond to clicks on the TextView.
textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setClickable(true);
You can also use other types of ClickableSpans such as URLSpan and ImageSpan, which provides more advanced functionality like opening a web page when clicked or displaying an image within the text.
4. Set the text of the TextView to the spannable string:
textView.setText(spannableString);
5. For better usability, you can also add a color for the text when it is clicked and a underline with
spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new UnderlineSpan(), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
6. Set the TextView clickable, to give the user a visual clue that the text is clickable.
textView.setClickable(true);
Here is a Complete Example –
in Activity Class –
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
in XML:
<TextView ... android:textColorLink="@drawable/your_selector" />
Please note that this is just an example and you will have to adjust it to suit your specific needs.
Also, you can use more advanced libraries like TextView LinkBuilder, to make it easier.
for more information, you can Visit Here. if you want to read our other posts you can check our blog section

