1

I need to show a clear button at the right of an edit text and hide it when the text is"". How can I do that.

I just need to know how to show and hide it when the text length>0 or =0, nothing more.

Dharvik shah
  • 1,969
  • 12
  • 27
Andrei Pascale
  • 232
  • 1
  • 3
  • 16
  • Possible duplicate of [Counting Chars in EditText Changed Listener](http://stackoverflow.com/questions/4310525/counting-chars-in-edittext-changed-listener) – Oleg Skidan Jul 28 '16 at 09:23
  • have a look at this http://www.devexchanges.info/2015/04/android-edittext-with-cross-button-in.html – Android Surya Jul 28 '16 at 09:30

6 Answers6

2

Here is your answer.

Use code and make button visibility on and off on textchange by check character length.

Community
  • 1
  • 1
Dharvik shah
  • 1,969
  • 12
  • 27
2

Take a look at below code, it may help you :

EditText etSearch = (EditText) view.findViewById(R.id.etSearch);

etSearch.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) {
                String str = etSearch.getText().toString();
                if (str.isEmpty()) {
                    btnClear.setVisibility(View.INVISIBLE);
                } else {
                    btnClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
Dang Nguyen
  • 354
  • 2
  • 9
2

Text-watcher may work for you

If(editText1.getText.toString.length>0)
 {
  if(button1.getVisibility == View.GONE)
  {
  button1.setVisibility(View.VISIBLE)
  }
}
else
{
 if(button1.getVisibility == View.VISIBLE)
  {
  button1.setVisibility(View.GONE)
  }
}
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
1

Set It's Visibility GONE from xml.

If(editText1.getText.toString.length>0)
 {
  if(button1.getVisibility == View.GONE)
  {
  button1.setVisibility(View.VISIBLE)
   }
}
else
{
 if(button1.getVisibility == View.VISIBLE)
  {
  button1.setVisibility(View.GONE)
  }
}
Android Surya
  • 544
  • 3
  • 17
Krishna Kachhela
  • 773
  • 1
  • 7
  • 24
1

Try this code

Field1.addTextChangedListener(new TextWatcher() {

     @Override
      public void afterTextChanged(Editable s) {}

     @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.length() != 0)
        // set the visibility
    }
     });
Android Surya
  • 544
  • 3
  • 17
DKV
  • 1,767
  • 3
  • 28
  • 49
1

You can use a TextWatcher, a type of Listener:

yourEditText.addTextChangedListener(new TextWatcher() {

      public void afterTextChanged(Editable s) {

        //Here, you can check for text size...
        int length = editText.toString().length();
         [...]



      }

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

      public void onTextChanged(CharSequence s, int start, int before, int count) {}
   });
}

}

Taken from this question.

Community
  • 1
  • 1
Aleksandar Stefanović
  • 1,583
  • 2
  • 20
  • 36