android - Create an Incomplete Button Stroke -


aim: stroke top , bottom.

what i've tried:

below copy of xml. i've tried following solution in this stack overflow answer. problem doesn't let me choose options of cutting off left , right 1dp per solution.

any ideas?

code:

<?xml version="1.0" encoding="utf-8"?>  <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true">         <shape >             <gradient                     android:startcolor="@color/secondarybuttonstartcolorselected"                     android:endcolor="@color/secondarybuttonendcolorselected"                     android:angle="270" />             <stroke                     android:width="@dimen/secondary_button_border_size"                     android:color="@color/secondarybuttonbordercolorselected" />          </shape>     </item>      <item android:state_focused="true" >         <shape>             <gradient                     android:startcolor="@color/secondarybuttonstartcolorselected"                     android:endcolor="@color/secondarybuttonendcolorselected"                     android:angle="270" />             <stroke                     android:width="@dimen/secondary_button_border_size"                     android:color="@color/secondarybuttonbordercolorselected"/>          </shape>     </item>  </selector> 

you create custom drawable handle you, you'd have set in code vs xml. here's quick , dirty version:

public class horizontalstrokedrawable extends drawable {     private paint mpaint = new paint();     private int mstrokewidth;      public horizontalstrokedrawable (int strokecolor, int strokewidth) {         mpaint.setcolor(strokecolor);         mstrokewidth = strokewidth;     }      @override     public void draw (canvas canvas) {         rect bounds = getbounds();         canvas.drawrect(0, 0, bounds.right, mstrokewidth, mpaint);         canvas.drawrect(0, bounds.bottom - mstrokewidth, bounds.right, bounds.bottom, mpaint);     }      @override     public void setalpha (int alpha) {         mpaint.setalpha(alpha);         invalidateself();     }      @override     public void setcolorfilter (colorfilter cf) {         mpaint.setcolorfilter(cf);         invalidateself();     }      @override     public int getopacity () {         return pixelformat.translucent;     } } 

now can set wherever need it:

view.setbackgrounddrawable(new horizontalstrokedrawable(mycolor, mystrokewidth)); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -