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;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
String text =
"First Click THIS and then THIS "
;
SpannableString ss =
new
SpannableString(text);
ClickableSpan clickableSpan1 =
new
ClickableSpan() {
public
void
onClick(View widget) {
Toast.makeText(MainActivity.
this
,
"First Clickable Text"
, Toast.LENGTH_SHORT).show();
}
};
ClickableSpan clickableSpan2 =
new
ClickableSpan() {
@Override
public
void
onClick(View widget) {
Toast.makeText(MainActivity.
this
,
"Second Clickable Text"
, Toast.LENGTH_SHORT).show();
}
};
ss.setSpan(clickableSpan1,
12
,
16
, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpan2,
26
,
30
, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}