android - Custom text span not working -
android - Custom text span not working -
i trying create span give rounded background text. however, draw method never called.
this code:
public class roundedbackgroundspan extends replacementspan { @override public void draw(canvas canvas, charsequence text, int start, int end, float x, int top, int y, int bottom, paint paint) { rectf rect = new rectf(x, top, x + measuretext(paint, text, start, end), bottom); paint.setcolor(color.blue); canvas.drawroundrect(rect, 100f, 30f, paint); paint.setcolor(color.white); canvas.drawtext(text, start, end, x, y, paint); } @override public int getsize(paint paint, charsequence text, int start, int end, paint.fontmetricsint fm) { homecoming math.round(measuretext(paint, text, start, end)); } private float measuretext(paint paint, charsequence text, int start, int end) { homecoming paint.measuretext(text, start, end); } } and how setting up:
spannable spannable = new spannablestring(introtext.gettext()); spannable.setspan(new roundedbackgroundspan(), 0, textview.gettext().length(), spanned.span_exclusive_exclusive); textview.settext(spannable); i don't have clue of why not working.
create shapes.xml following
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <stroke android:width="1dp" android:color="#000000" /> <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" /> <corners android:bottomrightradius="15dp" android:bottomleftradius="15dp" android:topleftradius="15dp" android:toprightradius="15dp"/> </shape> in xml file, utilize android:background="@drawable/shapes" text.
android textview spannablestring
Comments
Post a Comment