0

It must display "Correct" when the button text = "de" but when I click the button it says wrong but my button text is "de". Why (if) statement does not work despite of the button text is "de"?

public void bot1(View v) {
    Button choice1 = (Button) findViewById(R.id.button1);

    if(choice1.getText() == "de")
        Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
    else
       Toast.makeText(this, "Wrong", Toast.LENGTH_SHORT).show();
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37

1 Answers1

2

You have to use equals, not ==

public void bot1(View v){

    Button choice1 = (Button) findViewById(R.id.button1);
    if(choice1.getText().toString().equals("de"))
    Toast.makeText(this,"Correct",Toast.LENGTH_SHORT).show();
    else
   Toast.makeText(this,"Wrong",Toast.LENGTH_SHORT).show();
}
Curio
  • 1,331
  • 2
  • 14
  • 34