Solution 1 :
Well I will highly recommend you to debug and see as everything seems correct but I think it’s the issue with white-spaces
Try this
if(str1.equals(textview.trim())){button.setText("YES");}else{button.setText("NO");}
Problem :
This question has been asked severally and I collected all the answers and tried them but it is not really working for me.
I have a TextView which displays one of these names at a time:
Joseph99,
Thomas79,
Marry29,
Matthew21
Below this TextView I have a button. What I want is this, When TextView displays for example this name: Joseph99, i want the button to change its text to: Yes
If any other name is displayed on the TextView I want the button to say: NO.
For now the button actually does nothing I just want it to change text to either YES or NO depends on what is displayed on a TextView.
I have tried the following in the onCreate();
final textview = tv.getText().toString();
final String str1 = "Joseph99";
final String str2 = "Thomas79"
final String str3 = "Marry29"
final String str4 = "Matthew21"
if(str1.matches(textview)){
button.setText("YES");
}else{button.setText("NO");}
On the above code the text matches but I am getting a NO on a button even if the text on TextView is equal to str1. I also tried .equals like below:
if(str1.equals(textview)){
button.setText("YES");
}else{button.setText("NO");}
But I still get the NO even if the TextView has same text with str1.
I also tried this code:
if(!textview.getText().toString().matches("Joseph99");
And also this:
if(!textview.getText().toString().equals("Joseph99")
But the button text does not change, it is always NO, whether the TextView displays Joseph99 or not.
Note: The data which shows on TextView comes from SharedPreferences and it changes time by time.
There might be an error on my code but its three days now trying to figure out, but I have failed
Comments
Comment posted by Yaron Grushka
Can you try logging
Comment posted by Matthew
I have tried your code below still I get a NO
Comment posted by Yaron Grushka
What I asked was that you try using
Comment posted by Matthew
Okay i will do this soon had left for a relief,