-1

I want If the button has the text start execute start capture.

and if the button has the text stop execute stop capture.

if { //button text is start
   //start capture
} else if { //button text is stop
   // stop capture
}

how to compare text in buttons on android?

chohyunwook
  • 251
  • 2
  • 4
  • 9

4 Answers4

0
if(button.getText().toString().equalsIgnoreCase("start")){
//start capture
}else if (button.getText().toString().equalsIgnoreCase("stop")){
// stop capture
}

try this hope it helps

Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
0

Always use switch for nested if else statements

switch(button.getText().toString()){
case "start":
//start capture
break;
case "stop":
// stop capture
break;
}

It is always checking all conditions in nested if else statements. But in switch case it will go to the relevant case only, won't check all the conditions.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0
Button button = (Button) findViewById(R.id.btn);

String btnText = button.getText().toString();

if (btnText.equalsIgnoreCase("start")){

  //something here
}

-Or-

Button btn = (Button) findViewById(R.id.btn);;

if(btn.getText().toString().equalsIgnoreCase(text)){

   //something here

}
vijay chhalotre
  • 396
  • 2
  • 11
0
if(button1.getText().toString().equals("Start")){

}else if(button2.getText().toString().equals("Stop")){

}
Haider Ali
  • 122
  • 7