2

I want to create a login page, I used EditText to insert user info. I want to check EditText to see if it is Empty Invisible login Button, when inserted any character with user visible Login Button.
I tried the code shown below, but it did not not work for me :

//Show Login Button
String login_phoneString = login_PhoneText.getText().toString().trim();
if (login_phoneString.isEmpty()) {
    login_image.setVisibility(View.INVISIBLE);
} else {
    login_image.setVisibility(View.VISIBLE);
}

When EditText is empty, the button is invisible, and when set character in EditText again the login button is not shown.

How can I fix this problem ?

dinotom
  • 4,990
  • 16
  • 71
  • 139
Mohammad Nouri
  • 2,245
  • 4
  • 17
  • 34

5 Answers5

3

You want to show/hide the Login button base on the text of EditText so you need to listen for changing in EditText by use TextWatcher.
Use this code inside onCreate() method

login_PhoneText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            String login_phoneString = login_PhoneText.getText().toString().trim();
            if (login_phoneString.isEmpty()) {
                login_image.setVisibility(View.INVISIBLE);
            } else {
                login_image.setVisibility(View.VISIBLE);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Linh
  • 57,942
  • 23
  • 262
  • 279
1

Using trim()

if(et.getText().toString().trim().length() == 0) //empty

Using TextUtils

if(TextUtils.isEmpty(et.getText().toString().trim()) //Empty

Using isEmpty()

if(et.getText().toString().isEmpty()) //Empty

EDIT

You can do this :

//Show Login Button
String login_phoneString = login_PhoneText.getText().toString().trim();
if (TextUtils.isEmpty(login_phoneString) {
   login_image.setVisibility(View.INVISIBLE);
} else {
   login_image.setVisibility(View.VISIBLE);
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

Try to check like this way

    EditText edt = (EditText) findViewById(R.id.edittext);
    String abc = edt.getText().toString();
    if (abc.matches("")) {
        Toast.makeText(this, "enter something", Toast.LENGTH_SHORT).show();
        login_image.setVisibility(View.INVISIBLE);
        return;
    }
    else
    {
      login_image.setVisibility(View.VISIBLE);
     }
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0
String login_phoneString = login_PhoneText.getText().toString().trim();
if (login_phoneString.equals("")) {
    login_image.setVisibility(View.GONE);
} else {
    login_image.setVisibility(View.VISIBLE);
}

Use View.GONE instead of INVISIBLE. when INVISIBLE it is still clickable.

Anonymous
  • 4,470
  • 3
  • 36
  • 67
0

Use TextUtils.isEmpty():

if (TextUtils.isEmpty(yourEditText.getText().toString())) {
    //Empty
} else {
    //Not empty
}
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64