I want to set ClickableSpan
for a TextView
from another class. In ClickableSpan
I need to override two methods, updateDrawState()
and onClick()
. The former method is the same for all TextViews
, but the latter, onClick()
, is different for any TextView
. So I want write updateDrawState()
method only once (in the Commons class), not for any TextView
. How can I achieve this?
The following code blocks clearly explain what I want:
public class Commons {
...
public void makeSpannable(TextView tv, String text, int startIndex, int endIndex, ClickableSpan appendClickableSpan) {
SpannableString span = new SpannableString(text);
span.setSpan({
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ContextCompat.getColor(Activation.this, R.color.textViewClickable));
ds.setUnderlineText(false);
appendClickableSpan;
}}, startIndex, endIndex, 0);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(span, TextView.BufferType.SPANNABLE);
}
...
}
and
public class Main {
...
TextView textView = findViewById(R.id.textView1);
String text = "some sample text";
new Commons().makeSpannable(textView, text, 2, 6, new ClickableSpan() {
@Override
public void onClick(View widget) {
if (validator.checkValidation(tilCode)) ResendActivationEmail();
}
});
...
}