I set the part of the textView is clickable using SpannableString. But it is very difficult for me to detect a long press my textView.
private fun makeTextLink(textView: TextView, str: String, underlined: Boolean, color: Int?, action: (() -> Unit)? = null) {
val spannableString = SpannableString(textView.text)
val textColor = color ?: textView.currentTextColor
val clickableSpan = object : ClickableSpan() {
override fun onClick(textView: View) {
action?.invoke()
}
override fun updateDrawState(drawState: TextPaint) {
super.updateDrawState(drawState)
drawState.isUnderlineText = underlined
drawState.color = textColor
}
}
val index = spannableString.indexOf(str)
spannableString.setSpan(clickableSpan, index, index str.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.text = spannableString
textView.movementMethod = LinkMovementMethod.getInstance()
textView.highlightColor = Color.TRANSPARENT
}
CodePudding user response:
TextView textView = findViewById(R.id.text_view);
String text = "First Click THIS and then THIS ";
SpannableString ss = new SpannableString(text);
// creating clickable span to be implemented as a link
ClickableSpan clickableSpan1 = new ClickableSpan() {
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "First Clickable Text", Toast.LENGTH_SHORT).show();
}
};
// creating clickable span to be implemented as a link
// setting the part of string to be act as a link
ss.setSpan(clickableSpan1, 12, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
CodePudding user response:
private void setClickableSpan(Activity context, TextView textView, String mainString, String selectedString)
{
ModelSettingData settingData = new StorageUtil(context).getSettingData();
ClickableSpan termsAndConditionSpan = new ClickableSpan()
{
@Override
public void onClick(@NonNull View textView) {
// code hear
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP){
// Do something for lollipop and above versions
ds.setUnderlineText(false);
ds.setColor(getResources().getColor(R.color.TRANSPARENT));
}
}
};
SpannableString content = new SpannableString(mainString);
int startPosition = mainString.indexOf(selectedString);
int endPosition = mainString.lastIndexOf(selectedString) selectedString.length();
content.setSpan(termsAndConditionSpan, startPositionS1, endPositionS1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(content);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
}