Ahmed Shahryar Shery
Ahmed Shahryar Shery

Reputation: 23

I want my button to be click once and i want to disable double click

package com.example.shery.tictactoe;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
    TextView tv1,tv2;
    String field1 = tv1.getText().toString();
    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn8);
        tv1 = (TextView)findViewById(R.id.tv1);

        if (field1.equals("Turn X")){
            tv1.setTextColor(Color.GREEN);
        }else if (field1.equals("Turn O")){
            tv1.setTextColor(Color.GREEN);
        }

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(field1.equals("Turn X")){
                    btn1.setText("X");
                    tv1.setText("Turn O");
                } else {
                    btn1.setText("O");
                    tv1.setText("Turn X");
                }
            }
        });

I want my button to be click once and I want to disable double click so what will be method of doing this and how to do that in my code and want to stop double clicking this is my code below you can check. I want my button to be click once and I want to disable double click :

Upvotes: 1

Views: 49

Answers (2)

RipperJugo
RipperJugo

Reputation: 85

From what I understand, you want to disable de double click on the button. This is not possible because Android queues the clicks, but there's a trick I always use: Storing the last time it was clicked:

private long btn1ClickTime= 0; //local variable declaration


findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (SystemClock.elapsedRealtime() - btn1ClickTime< 1000){
            return;//do nothing
        }
        btn1ClickTime= SystemClock.elapsedRealtime();
        //write your code here
    }
}

Upvotes: 0

Chris Stillwell
Chris Stillwell

Reputation: 10547

Just call view.setEnabled(false); in your onClickListener to disable the view after the first click.

Upvotes: 1

Related Questions