I have this Selector defined:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- PRESSED -->
<item android:state_pressed="true"
android:drawable="@drawable/backarrow_blueshiny" />
<!-- FOCUSED -->
<item android:state_focused="true"
android:drawable="@drawable/backarrow_blackshiny" />
<!-- DEFAULT -->
<item android:drawable="@drawable/backarrow_blackshiny" />
</selector>
and it's used with this button:
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottomborder_glossy">
<!-- BACK -->
<ImageButton
android:id="@+id/filter_button_back"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:src="@drawable/selector_back_button"
android:background="#00000000"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
and the onTouch event consists of:
public boolean onTouch(View v, MotionEvent event)
{
final int actionPerformed = event.getAction();
final int widgetID = v.getId();
if (actionPerformed == MotionEvent.ACTION_UP)
{
switch (widgetID)
{
case R.id.filter_button_back:
{
this.finish();
break;
}
}
}
return false;
}
what this button does is exit the current activity this.finish()
However, in my testing the button doesn't always switch to "backarrow_blueshiny" - namely when pressing very fast.
So the problem is that the selector fires slower than the onTouch(MotionEvent.ACTION_UP) event.
Is there anything I can do to make sure that the selector isn't "laggy" ?
Try to set your view as pressed programmatically in OnTouchListener:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
button.setPressed(true);
break;
case MotionEvent.ACTION_UP:
// optional, should work without next line
// button.setPressed(false);
break;
}
return false;
}
I had the same problem. In the emulator it doesn't even changed button colour. It's caused by the "onTouch". If you use onClickListener instead of onTouchListener it will react as expected.
As I can see you are not using any multitouch capability so wy dont you use the onClick and get rid of the
if (actionPerformed == MotionEvent.ACTION_UP)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With