I was tiring to underline a specific word in the text with a dotted or dashed line with and also Clickable Span.
I haven't found a solution can someone help me, please.
SpannableStringBuilder sb = new SpannableStringBuilder(text); List listmot = new ArrayList();
listmot=db.getAlldef();
for(int i=0;i<listmot.size();i++)
{
String mot = (listmot.get(i)).get_mot();
final String def = (listmot.get(i)).get_definition();
Log.v(null, mot);
Pattern p = Pattern.compile(mot, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
while (m.find()){
// Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/font.ttf");
//sb.setSpan(new CustomTypefaceSpan("",tf), m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// Toast.makeText(getApplicationContext(), def,Toast.LENGTH_LONG).show();
int[] values = new int[2];
widget.getLocationOnScreen(values);
Log.d("X & Y",values[0]+" "+values[1]);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AndroidSQLiteTutorialActivity.this);
alertDialog.setMessage(def);
alertDialog.setCancelable(true);
AlertDialog alert11 = alertDialog.create();
alert11.setCanceledOnTouchOutside(true);
alert11.show();
}
}, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textjson.setText(sb);
textjson.setMovementMethod(LinkMovementMethod.getInstance());
EDIT: I have updated solution. Sometimes that drawBackground method in solution with LineBackgroundSpan just haven't been called for no reasons. New version works all the time and looks much clearer.
I met the same problem and i solved it by combining this solutions:
The result code looks like this:
To make your underline look the same on all densities set that dimens in dp
And this is how you use it:
And be sure that you turn off hardware acceleration on textView that will show the underline span. https://stackoverflow.com/a/24467362/5373069
EDIT: if you see ways to improve this solution i would appreciate any good points.