Android Studio : Press several buttons

Viewed 23

I want to press several buttons. For example, you can press button3 after pressing button1 (you cannot press button3 if you do not press button1). I want it to work like the code below, but I don't know how to write it correctly.

public void onClick(View view){
    if(view.getId() == R.id.button1){
        button1.setBackground(ContextCompat.getDrawable(this, R.drawable.design_btn_red_on));
        if(view.getId() == R.id.button3){
            //Blink button3
            if(view.getId() == R.id.button10){
                button3.setBackground(ContextCompat.getDrawable(this, R.drawable.design_btn_default_on));
            }

        }
    }
    if(view.getId() == R.id.button2){
        button2.setBackground(ContextCompat.getDrawable(this, R.drawable.design_btn_green_on));
    }
}

Above code: press button1 to turn button1 red and then press button3 to turn button3 blink. And then press button 10 and button3 turns white.

1 Answers

Pardon me it's been a while since I've used android studio Not very sure if this will work: By default loading the page

For your button3:

    Button button = findViewById(R.id.button)
    button.setEnabled(false);

Then during your if else when button 1 is clicked

    if (onclick button1 trigger)  //I can't recall what is the action button code
    {
     button.setEnabled(true);  //Trigger your button3 to be enabled
    }
    else
    {

    }

For kotlin:

// to disable clicks myButton.isClickable = false

// to disable button myButton.isEnabled = false

// to enable clicks myButton.isClickable = true

// to enable button myButton.isEnabled = true

Related