Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop users from pressing two buttons at once?

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.

like image 424
ldam Avatar asked May 16 '13 09:05

ldam


Video Answer


1 Answers

android:splitMotionEvents="false" for holder of buttons

like image 94
Roman Trokhymets Avatar answered Oct 01 '22 12:10

Roman Trokhymets