2

Possible Duplicate:
onPress/onRelease in Android

When a user holds a button the button changes color, and when the user leaves the button, the button returns to it's normal state.

Is there an event that captures this ?

I tried searching for something like button.onStateChanged, but no luck..

EDIT: When the user long clicks I want to start the autoincrement

// Auto increment for a long click
        increment.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View a) {
                autoIncrement = true;

                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                repeatUpdateHandler.post(mRepeatUpdateHandler);

                return false;
            }
        });

And when the user leaves the button with his finger I want to stop the autoincrement

increment.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {

                //Stop autoincrement

                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                autoIncrement = false;
            }

            return false;
        }
    });

This is the handler:

RepetetiveUpdater mRepeatUpdateHandler = new RepetetiveUpdater();
 ....

class RepetetiveUpdater implements Runnable {
    public void run() {
        if (autoIncrement) {
            increment();
            repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
                    REPEAT_DELAY);
        } else if (autoDecrement) {
            decrement();
            repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
                    REPEAT_DELAY);
        }
    }
}

But this only works when the user lifts his finger above the button, when he drags it to somewhere else on the screen, the counter keeps running, any ideas?

Community
  • 1
  • 1
Robby Smet
  • 4,649
  • 8
  • 61
  • 104

3 Answers3

4

I don't know if my code below does exactly what you're trying to do. The increment method will be called until the user stops clicking or his finger isn't situated in the view's bounds. All is done in the onTouch callback:

    final Rect r = new Rect();
    // this is the platform's default timeout for recognizing a long press
    final long LONG_PRESS_TIMOUT = ViewConfiguration.getLongPressTimeout();
    increment.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            // get the View's Rect relative to its parent
            v.getHitRect(r);
            // offset the touch coordinates with the values from r
            // to obtain meaningful coordinates
            final float x = event.getX() + r.left;
            final float y = event.getY() + r.top;
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // simulate a long press
                // if the user stops pressing before the long press timeout
                // expires this will be canceled
                repeatUpdateHandler.postDelayed(mRepeatUpdateHandler,
                        LONG_PRESS_TIMOUT);
                break;
            case MotionEvent.ACTION_MOVE:
                // if the touch coordinates are not in the Views' rect then
                // cancel any Runnable
                if (!r.contains((int) x, (int) y)) {
                    repeatUpdateHandler
                            .removeCallbacks(mRepeatUpdateHandler);
                }
                break;
            case MotionEvent.ACTION_UP:
                // the user raised his finger, cancel the Runnable
                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                break;
            }
            return false;
        }
    });

And the Runnable part:

class RepetetiveUpdater implements Runnable {
    public void run() {
        increment();
        repeatUpdateHandler.postDelayed(mRepeatUpdateHandler, 1000);
    }
}

int i = 0;

public void increment() {
    Log.e("INCREMENT", "Value " + (++i));
}
user
  • 86,916
  • 18
  • 197
  • 190
0

You can do it in a different way use onTouch() or onFocusChange() listeners for your button.. I am not sure you just try once .

E.g :

        button.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

        }
    });

    button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    });
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
0
@Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  // TODO Auto-generated method stub

  textX.setText("x: " + String.valueOf(motionEvent.getX()));
  textY.setText("y: " + String.valueOf(motionEvent.getY()));

  int action = motionEvent.getAction();

  switch (action){
  case MotionEvent.ACTION_DOWN:
   textEvent.setText("ACTION_DOWN");      
   break;
  case MotionEvent.ACTION_MOVE:
   textEvent.setText("ACTION_MOVE");
   break;
  case MotionEvent.ACTION_UP:
   textEvent.setText("ACTION_UP");          // you need this!
   break;
  case MotionEvent.ACTION_CANCEL:
   textEvent.setText("ACTION_CANCEL");
   break;
  default:
   textEvent.setText("Unknown!");
  }

  return true; //means event have been processed
 }
Aditya Nikhade
  • 1,373
  • 10
  • 15
  • 1
    Voted down because it is a pure code-only answer. Write more like where this method should be placed and how it works. Be descriptive and share your knowledge, not your code. Change that and I will revert or maybe even vote up, too. – WarrenFaith Sep 11 '12 at 11:37