2
I have to test a certain Textview to see if it is filled in the onCreate() of Activity and if it is filled, I have to change the image of an Imagebutton. I’m using the following code for this:
if (txt_photo_path.getText().toString() != null) {
btnenviarfoto.setImageResource(R.drawable.enviarfotochecked);
}
However, the code is always running. I decided to test to see why Textview is always filled, using the following, inside if:
Log.w("txt_photo_path", txt_photo_path.getText().toString());
What I get in my log is below
09-05 12:56:53.161 14726-14726/com.ufscar.ufscar.df100fogo W/txt_photo_path﹕ [ 09-05 12:56:53.381 576: 591 I/ActivityManager ]
Why is this happening? How to know if Textview is really empty or not?
txt_photo_path.getText().toString() != ""by default theTextViewis always filled with the empty string "".– Jorge B.
txt_photo_path.getText().toString().isEmpty()ortxt_photo_path.getText().toString().length() == 0ortxt_photo_path.getText().toString().equals("")is better, never use comparator==withString, take care of the difference betweenStringin theString Pooland instances thereof.– Wakim
@Walkim is right, I forgot
.equals(""). then you can use it like this:if (!txt_photo_path.getText().toString().equals("")) {with the exclamation mark on the back that means NOT equals.– Jorge B.
I used
(!txt_photo_path.getText().toString().isEmpty())and it worked. Thank you all.– Borda
@Wakim formulates an answer!
– Jorge B.