android - Using a background drawable selector on custom button -
i extending "button" create custom button adding functionality -- , currently, background drawable not changing on touch. here sample code shows doing:
/src/custombutton.java
public class custombutton extends button { public custombutton(final context context) { this(context, null); } public custombutton(final context context, final attributeset attrs) { this(context, attrs, 0); } public custombutton(final context context, final attributeset attrs, final int defstyle) { super(context, attrs, defstyle); } }
/res/layout/myview.xml
<com.blah.controls.custombutton android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/b_gradient_states" android:text="button" />
/res/drawable/b_gradient_states
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/b_gradient_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/b_gradient" android:state_pressed="false" /> </selector>
** note **if change
<com.blah.controls.custombutton...
to
<button...
the touch states work expected...
pskink said in comment in question:
why in ctor(contexr) call super(context, null) , in ctor(context, attributeset) use super(context, attributeset, int)
and that's wrong...
public custombutton(final context context, final attributeset attrs) { this(context, attrs, 0); }
should be:
public custombutton(final context context, final attributeset attrs) { super(context, attrs); }
Comments
Post a Comment