I have a SpannableStringBuilder
object with a ClickableSpan
link like this,
SpannableStringBuilder ssb = new SpannableStringBuilder("Hi StackOverflow!");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
ssb.setSpan(clickableSpan, 3, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
This works fine when I set it to a TextView
and after using textView.setMovementMethod(LinkMovementMethod.getInstance())
When I set the ssb
object to the MaterialAlertDialogBuilder
,
new MaterialAlertDialogBuilder(this)
.setTitle("My Dialog")
.setMessage(ssb)
.show();
It displays as a clickable link but cannot actually click it
I couldn't find a way to use setMovementMethod
for the message in MaterialAlertDialogBuilder
. Is there any way to make it clickable?
Here is code snippet to make your spannable content clickable, give it a try.
SpannableString s = new SpannableString(msg); // Here msg should have url to enable clicking
Linkify.addLinks(s, Linkify.ALL);
After that put your alertdialog code here
//Alert dialog code
Then get id of textview of your MaterialAlertDialog, below line of code must be called after dialog.show() like this,
((TextView)dialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());