Sagar Duhan
Sagar Duhan

Reputation: 257

How to write code so that Button is only clickable when condition is true?

How can I write code in java so that when following IF Condition is true:

if (!online.equals(offline)){

}

Then only the

a1.setOnClickListener(new View.OnClickListener() {
**algorithm**
}

gets activated else the button stays unclickable.

NOTE: Both "online" and "offline" are String variable.

Upvotes: 0

Views: 156

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

Use the enabled feature. myButton.setEnabled(false) makes it unclickable, myButton.setEnabled(true) makes it clickable. You can even use state list drawables to make disabled buttons look different.

Upvotes: 9

Related Questions