5

Is there a way to only register a single button press? At the moment in my activity I have two buttons that each start another activity - at the moment if I press them at the same time both activities start, one over the other. I want to prevent this and only have one OR the other running at any given time. I thought about disabling multitouch but that feels kind of hacky to me.

EDIT: I've tried to do this:

public class MainActivity extends Activity {

    Button btn_send;
    Button btn_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_send = (Button) findViewById(R.id.btn_send);
        btn_receive = (Button) findViewById(R.id.btn_receive);
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.equals(btn_send)) {
                    if (isWifiConnected()) {
                        btn_receive.setClickable(false);
                        startActivity(new Intent(MainActivity.this,
                                SendActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Wifi connection required", Toast.LENGTH_SHORT)
                                .show();
                    }
                } else if (v.equals(btn_receive)) {
                    if (isWifiConnected()) {
                        btn_send.setClickable(false);
                        startActivity(new Intent(MainActivity.this,
                                ReceiveActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Wifi connection required", Toast.LENGTH_SHORT)
                                .show();
                    }
                }

            }

        };
        btn_send.setOnClickListener(listener);
        btn_receive.setOnClickListener(listener);
    }

    @Override
    public void onPause() {
        btn_send.setClickable(true);
        btn_receive.setClickable(true);
        super.onPause();
    }
}

I can still press both buttons at the same time on my Galaxy S2 GT-i9100.

ldam
  • 4,412
  • 6
  • 45
  • 76

7 Answers7

8

android:splitMotionEvents="false" for holder of buttons

Roman Trokhymets
  • 1,102
  • 10
  • 10
1

Register the same onClickListener for both the button and call setEnabled ( false ) to the latter when the former has been clicked

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • and call `setEnabled(true)` in `onPause` in case user returns to this `Activity`. Actually using a single `onClickListener` is not needed. – MaciejGórski May 16 '13 at 09:53
  • I agree, but why have more the one if it is not strictly needed? – Blackbelt May 16 '13 at 09:55
  • That's a personal preference. I don't like `ifs` or `switches` and avoid them when possible by using object oriented code. Of course when that makes sense... – MaciejGórski May 16 '13 at 09:57
  • A month late, haven't had time to work on this. Thanks for the answer. I've tried this and I can still manage to press both buttons and start both activities at the same time. – ldam Jun 09 '13 at 17:00
1

First of all, create a class to handle touch events, like the below:

public class SingleClickTouchListener implements OnTouchListener {
    private boolean mOneViewClicked = false;

    /**
     * Holds the clicked view. Only allow click in other views when this view is no longer clicked.
     */
    private Object mClickedView = null;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvt) {
        boolean consumed = false;

        switch (motionEvt.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mOneViewClicked) {
                mClickedView = view;
                mOneViewClicked = true;
            } else {
                consumed = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mClickedView == view) {
                mOneViewClicked = false;
                consumed = false;
            } else {
                consumed = true;
            }
            break;
        default:
            // No action required.
            break;
        }

        return consumed;
    }
}

Then, create an instance of this class:

private SingleClickTouchListener mSingleClickTouchListener = new SingleClickTouchListener();

And now pass the same instance to all views (or buttons) that you don't want to allow be pressed simultaneously.

view1.setOnTouchListener(mSingleClickTouchListener);
view2.setOnTouchListener(mSingleClickTouchListener);
view3.setOnTouchListener(mSingleClickTouchListener);
1

This worked for me

boolean hasRunOnce = false;
ImageButton clickExample1, clickExample2;

    clickExample1 = (ImageButton) findViewById(R.id.btnExample1);
    clickExample1.setOnClickListener(this);

    clickExample2 = (ImageButton) findViewById(R.id.btnExample2);
    clickExample2.setOnClickListener(this);


}

@Override
public void onClick(View v) {


    if (hasRunOnce == false) {
        hasRunOnce = true;  //Stop users clicking buttons simultaneously

        switch (v.getId()) {
        case R.id.btnExample1:
            //do stuff
            break;
        case R.id.btnExample2:
            //do stuff
            break;

        }
    }
}



@Override
protected void onResume() {
    hasRunOnce = false;

    super.onResume();
}

}

Liam
  • 11
  • 1
0

use same click listner. ie, implement onClickListner for the class and button.setOnClickListner(this) for both the buttons.

Jithu
  • 1,478
  • 1
  • 13
  • 21
0

Inside your common onclick-listener

if(oneClick){
   oneClick = false;
   //launch the activity
}

,and reset the flag in onPause,or wherever appropriate.

Kunal Khaire
  • 307
  • 2
  • 7
0

Try this

button1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                button2.setClickable(false);

                //launch first activity
            }
        });

button2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                button1.setClickable(false);

                //launch second activity
            }
        });
Oam
  • 912
  • 6
  • 7