Custom Span Underline Text with Dotted line Android -
i tiring underline specific word in text dotted or dashed line , clickable span.
i haven't found solution can 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: have updated solution. drawbackground method in solution linebackgroundspan haven't been called no reasons. new version works time , looks clearer.
i met same problem , solved combining solutions:
- this https://stackoverflow.com/a/21097663/5373069 and
- this https://stackoverflow.com/a/6104138/5373069
the result code looks this:
private static class dottedunderlinespan extends replacementspan { private paint p; private int mwidth; private string mspan; private float mspanlength; private boolean mlengthiscached = false; public dottedunderlinespan(int _color, string _spannedtext){ p = new paint(); p.setcolor(_color); p.setstyle(paint.style.stroke); p.setpatheffect(new dashpatheffect(new float[]{mdashpatheffect, mdashpatheffect}, 0)); p.setstrokewidth(mstrokewidth); mspan = _spannedtext; } @override public int getsize(paint paint, charsequence text, int start, int end, paint.fontmetricsint fm) { mwidth = (int) paint.measuretext(text, start, end); return mwidth; } @override public void draw(canvas canvas, charsequence text, int start, int end, float x, int top, int y, int bottom, paint paint) { canvas.drawtext(text, start, end, x, y, paint); if(!mlengthiscached) mspanlength = paint.measuretext(mspan); // https://code.google.com/p/android/issues/detail?id=29944 // canvas.drawline can't draw dashes when hardware acceleration enabled, // canvas.drawpath can path path = new path(); path.moveto(x, y + moffsety); path.lineto(x + mspanlength, y + moffsety); canvas.drawpath(path, this.p); } } to make underline same on densities set dimens in dp
mstrokewidth = context.getresources().getdimension(r.dimen.stroke_width); mdashpatheffect = context.getresources().getdimension(r.dimen.dash_path_effect); moffsety = context.getresources().getdimension(r.dimen.offset_y); and how use it:
dottedunderlinespan dottedunderlinespan = new dottedunderlinespan(0xff00ff00, spannedtext); spannablestring.setspan(dottedunderlinespan, startoftext, endoftext, spanned.span_inclusive_inclusive); and sure turn off hardware acceleration on textview show underline span. https://stackoverflow.com/a/24467362/5373069
edit: if see ways improve solution appreciate points.
Comments
Post a Comment