android - Change undeline color of Textview -
i have 7 textview
1 selectable @ time,selected textview
should contain underline,i want change underline color(not textview
color).
i using following code underline textview
,
addunderlinetotext.setpaintflags(addunderlinetotext.getpaintflags() | paint.underline_text_flag);
i using following line remove under line.
if ((removeunderline[i].getpaintflags() & paint.underline_text_flag) > 0) { textview[i].setpaintflags(textview[i].getpaintflags() & (~paint.underline_text_flag)); }
first defined custom attributes easy customization in xml layout files
<declare-styleable name="underlinedtextview" > <attr name="underlinewidth" format="dimension" /> <attr name="underlinecolor" format="color" />
and custom textview class
public class underlinedtextview extends textview { private rect lineboundsrect; private paint underlinepaint; public underlinedtextview(context context) { this(context, null, 0); } public underlinedtextview(context context, attributeset attrs) { this(context, attrs, 0); } public underlinedtextview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(context, attrs, defstyleattr); } private void init(context context, attributeset attributeset, int defstyle) { float density = context.getresources().getdisplaymetrics().density; typedarray typedarray = context.obtainstyledattributes(attributeset, r.styleable.underlinedtextview, defstyle, 0); int mcolor = typedarray.getcolor(r.styleable.underlinedtextview_underlinecolor, 0xffff0000); float mstrokewidth = typedarray.getdimension(r.styleable.underlinedtextview_underlinewidth, density * 2); typedarray.recycle(); lineboundsrect = new rect(); underlinepaint = new paint(); underlinepaint.setstyle(paint.style.stroke); underlinepaint.setcolor(mcolor); //color of underline underlinepaint.setstrokewidth(mstrokewidth); } @colorint public int getunderlinecolor() { return underlinepaint.getcolor(); } public void setunderlinecolor(@colorint int mcolor) { underlinepaint.setcolor(mcolor); invalidate(); } public float getunderlinewidth() { underlinepaint.getstrokewidth() } public void setunderlinewidth(float mstrokewidth) { underlinepaint.setstrokewidth(mstrokewidth); invalidate(); } @override protected void ondraw(canvas canvas) { int count = getlinecount(); final layout layout = getlayout(); float x_start, x_stop, x_diff; int firstcharinline, lastcharinline; (int = 0; < count; i++) { int baseline = getlinebounds(i, lineboundsrect); firstcharinline = layout.getlinestart(i); lastcharinline = layout.getlineend(i); x_start = layout.getprimaryhorizontal(firstcharinline); x_diff = layout.getprimaryhorizontal(firstcharinline + 1) - x_start; x_stop = layout.getprimaryhorizontal(lastcharinline - 1) + x_diff; canvas.drawline(x_start, baseline + mstrokewidth, x_stop, baseline + mstrokewidth, underlinepaint); } super.ondraw(canvas); } }
then it's usage simple
<some.package.underlinedtextview android:id="@+id/tvtest" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="10dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:gravity="center" android:text="this demo text" android:textsize="16sp" app:underlinecolor="#ffc112ef" app:underlinewidth="3dp"/>
Comments
Post a Comment