-3

I have a submit button where I need to set the visibility of the button based on if a edittext field is empty or not.

I have below snippet in the onCreate method of the activity

submitIssue.setVisibility(View.VISIBLE);

if(issueTxt.getText().toString() == ""){
    submitIssue.setVisibility(View.INVISIBLE);
}else{
    submitIssue.setVisibility(View.VISIBLE);
}

On launching the application the button displays even though the edittext is empty. What is wrong with my approach?

James Z
  • 12,209
  • 10
  • 24
  • 44
Float
  • 263
  • 2
  • 14

6 Answers6

0

Try this-:

if(TextUtils.isEmpty(issueTxt.getText().toString()))
{
   submitIssue.setVisibility(View.INVISIBLE);
}
else
{
   submitIssue.setVisibility(View.VISIBLE);
}
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

Use like this

 editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                   if (s.toString().length()<1){
                      button.setVisibility(View.GONE)
                   }else button.setVisibility(View.VISIBLE)
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
0

Use Android provides TextUtils.isEmpty() utility method:

if(TextUtils.isEmpty(issueTxt.getText().toString())){
    submitIssue.setVisibility(View.INVISIBLE);
} else {
    submitIssue.setVisibility(View.VISIBLE);
}
AlexTa
  • 5,133
  • 3
  • 29
  • 46
0

Try this one

   if(editText.getText().toString().trim().equals("")){

}

Or

if(editText.getText().toString().trim().length==0){
}

Always try to use trim() function when checking edittext is empty or not because if you use without trim() function than space(" ") can create issue for you...

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               // Add your code here
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

you can use TextChangeLister for other process.

Lokesh Desai
  • 2,607
  • 16
  • 28
0

You have to use TextWatcher() for achieve this.

and For Matching string you should use equals() method instead od ==

eg :

String1.equals(String2)

Refer the Below Links:

TexWatcher fo Android

TextChangerListener Android

You can use TextWatcher for your edittext,

eg:

 editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                   if(TextUtils.isEmpty(issueTxt.getText().toString()))
                   {
                        submitIssue.setVisibility(View.INVISIBLE);
                   }
                   else
                   {
                        submitIssue.setVisibility(View.VISIBLE);
                   } 

            }

            @Override
            public void afterTextChanged(Editable s) {
                // You can do Validation here too
            }
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

Try this:

submitIssue.setVisibility(View.VISIBLE);

    if(issueTxt.getText().toString().length() == 0){
        submitIssue.setVisibility(View.INVISIBLE);
    }else{
        submitIssue.setVisibility(View.VISIBLE);
    }
Shree
  • 354
  • 2
  • 21