-2

How do I check the content of a TextView?

At first I declare my TextView (MainActivity):

private TextView contentTxt1;
contentTxt1 = (TextView)findViewById(R.id.scan_text1);

Set the Text (strings.xml):

<string name="scan_text">Lorem</string>

Then I check its content (MainActivity):

if(contentTxt1.getText() == "Lorem"){

//Do Something
}

But when I debug my application now, the variables watcher shows me the following:

contentTxt1.getText() == "Lorem" = false

Okay, so it seems the content isn't "Lorem", maybe it's empty. But at the same time it shows me:

contentTxt1.getText() == "Lorem"

("Lorem is green")

What should I do now? Am I checking it wrong? I guess it's a simple but stupid mistake by me.

halfer
  • 19,824
  • 17
  • 99
  • 186
Feelix93
  • 1
  • 1

1 Answers1

0

You must use the .equals() method instead of == to compare two strings.

More: https://www.baeldung.com/java-compare-strings