Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector reacts slowly - sometimes not firing at all before starting next activity

Tags:

android

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" ?

like image 733
Someone Somewhere Avatar asked Feb 25 '23 01:02

Someone Somewhere


2 Answers

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;
}
like image 113
gingo Avatar answered Apr 27 '23 03:04

gingo


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)
like image 32
dinigo Avatar answered Apr 27 '23 02:04

dinigo