0

i have a button in android.If i hold the button a message should go and while releasing another message should go but Sometimes still holding a button also release is happening and release message is going.

Here is my code for button:-

open_curtain.setOnTouchListener(new View.OnTouchListener() {
                              @Override
                              public boolean onTouch(View v, MotionEvent event) {

                                  switch (event.getAction() & MotionEvent.ACTION_MASK) {


                                          case MotionEvent.ACTION_DOWN:

                                             v.setSelected(true);

                                              DeviceDataClassTwo obj = (DeviceDataClassTwo) v.getTag();
                                              new ButtonOPressed(obj).execute();


                                              break;

                                          case MotionEvent.ACTION_UP:
                                           case MotionEvent.ACTION_CANCEL:
                                              //it comes here when i move my finger more then 100 pixels
                                               v.setSelected(false);


                                                  obj = (DeviceDataClassTwo) v.getTag();
                                                  new ButtonORelease(obj).execute();

                                              Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                                              vibe.vibrate(140);

                                              break;



                                  }

                                  return true;
                              }
                          });

1 Answers1

0

In the cod eyou posted you have one error, two "switch"cases are not corretly separated.

You should change this:

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
    .
    .
    .
    break;

To the following:

case MotionEvent.ACTION_UP:
    break;
case MotionEvent.ACTION_CANCEL:
    .
    .
    .
    break;

If you are going to leave the case "MotionEvent.ACTION_UP" consider moving this to the "default" case. The "switch" should then look like this:

switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        .
        .
        .
        break;
    case MotionEvent.ACTION_CANCEL:
        .
        .
        .
        break;
    default:
        break;