0

I'm using textChangedListener on an edit text to show/hide a floating action button.

Everything pretty much works as expected. If edit text is not empty then show fab, if empty then hide fab.

However I have noticed that while using backspace, to delete entered text, deleting the first space triggers the hide fab, then quickly fab shows, since editText is not empty.

I have looked around and haven't seen anything regarding this. So I must be doing something wrong, but I'm not sure what.

Below is my implementation. I have tried and all give me the same behavior if(editText.getText().toString().isEmpty())

if(editText.getText().toString().length()==0))

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

editText.addTextChangedListener(new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count)     {
    Log.d(TAG, "TEXT CHANGED onTextChanged" + editText.getText().toString());


    // TODO Auto-generated method stub
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    Log.d(TAG, "TEXT CHANGED beforeTextChanged" + editText.getText().toString());


    // TODO Auto-generated method stub
  }

  @Override
  public void afterTextChanged(Editable s) {
    Log.d(TAG, "TEXT CHANGED afterTextChanged" + editText.getText().toString());
   // s =  editText.getText().toString(); editText.getText().toString()
    if (s.toString().isEmpty()) {
      fab.setVisibility(View.INVISIBLE);
      fab.setAnimation(slideDownAnim);
      slideDownAnim.start();
    }else{
      fab.setVisibility(View.VISIBLE);
      fab.setAnimation(slideUpAnim);
      slideUpAnim.start();
    }
    // TODO Auto-generated method stub
  }
});

Thanks in advance

***UPDATE**** Found answer which solves my problem here. TextWatcher events are being fired multiple times

Community
  • 1
  • 1
  • ahh found the answer that worked for me on another thread.. http://stackoverflow.com/questions/17535415/textwatcher-events-are-being-fired-multiple-times – user3246849 Dec 09 '15 at 17:56

1 Answers1

0

try this

editText.addTextChangedListener(new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count)     {
    Log.d(TAG, "TEXT CHANGED onTextChanged" + editText.getText().toString());

    if (s.toString().isEmpty()) {
      fab.setVisibility(View.INVISIBLE);
      fab.setAnimation(slideDownAnim);
      slideDownAnim.start();
    }else{
      fab.setVisibility(View.VISIBLE);
      fab.setAnimation(slideUpAnim);
      slideUpAnim.start();
    }
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    Log.d(TAG, "TEXT CHANGED beforeTextChanged" + editText.getText().toString());


    // TODO Auto-generated method stub
  }

  @Override
  public void afterTextChanged(Editable s) {
    Log.d(TAG, "TEXT CHANGED afterTextChanged" + editText.getText().toString());
   // s =  editText.getText().toString(); editText.getText().toString()

    // TODO Auto-generated method stub
  }
});
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • Deepak, thanks for the reply, but I had already tried that and odd behavior remains. The odd behavior only exists once the first space is deleted.I guess I could try some hacky attempt at checking the position of the first space and doing something there to avoid the button hiding and showing again, but that seems kinda silly. In any case thanks again – user3246849 Dec 09 '15 at 17:25