Search code examples
androiduser-interfaceactionbarsherlockimagebuttontouch-event

Android onTouch must be triggered twice in order to trigger actionBar.show() and button position change


I'm encountering a weird little problem with my onTouchEvent. When you touch the edge of the screen, it's supposed to show the action bar (using ActionbarSherlock, not the android actionbar), and move the button "buttonClick" down to accommodate the height of the action bar.

This works, but only if you touch twice. On the first touch, the action bar shows, and on the second touch, the button moves down. I have no idea why it doesn't do both during the first touch, and my searches around here have only turned up people having problems with it not firing at all, or a couple had problems where the onTouch was only firing twice, never just once.

My onTouchEvent() code is below, I'd really appreciate if somebody could help me understand why it's doing things this way.

public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.v(TAG, "New Touch");
            Log.v(TAG, "Screen Width: "+ screen_width);
            if (x >= (screen_width - 95)){
                int actionHeight = getSupportActionBar().getHeight();
                Log.v(TAG, ""+actionHeight);
                thumbParams.setMargins(0,actionHeight,0,0);
                thumbClick.setLayoutParams(thumbParams);
                params.setMargins(0,actionHeight,0,0);
                    buttonClick.setLayoutParams(params);
                getSupportActionBar().show();

            }
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }

return false;
}

Solution

  • The specific problem that was causing all of this was that the value for getSupportActionBar().getHeight() was 0 at the point I was trying to assign it to the margin. I tried a bunch of overly-elaborate ways to fix this, but it turned out in the end, all I had to do was move everything but getSupportActionBar().show(); into the ACTION_UP case.

    There can be a slight noticible lag if they leave their finger there too long, but most of the time it appears instantaneous, and it works on the first click now! I've put my adjusted code below in case my explanation is poorly worded.

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int x = (int)event.getX();
                int y = (int)event.getY();
                if (x >= (screen_width - 95)){
                    getSupportActionBar().show();
                }
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                actionHeight = getSupportActionBar().getHeight();
                Log.v(TAG, ""+actionHeight);
    
                thumbParams.setMargins(0, actionHeight, 0, 0);
                params.setMargins(0, actionHeight, 0, 0);
                thumbClick.setLayoutParams(thumbParams);
                buttonClick.setLayoutParams(params);
        }
    
    return false;
    }