-1

I need to show to Toolbar menu item when EditText gains focus and hide it when EditText lose focus.

I try to implement using setOnFocusChangeListener on EditText like below:

edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
           if (hasFocus) {
              sendMenuItem.setVisible(true);
           } else {
              sendMenuItem.setVisible(false);
           }
      }
});

but menu item is show and hide continuously as onFocusChange() is calling multiple times.

onFocusChange() is calling multiple times .

It is strange Logcat shows me following warning:

requestLayout() improperly called by android.support.v7.widget.ActionMenuView

is there any other way to achieve this?

Uniruddh
  • 4,427
  • 3
  • 52
  • 86
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78

1 Answers1

0

You can also use TextWatcher to know if anything is entered in EditText or not. And then show/hide your MenuItem.

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

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        textView.setVisibility(View.VISIBLE);
    }

    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
          sendMenuItem.setVisible(true);
       } else {
          sendMenuItem.setVisible(false);
       }
    }
});
Uniruddh
  • 4,427
  • 3
  • 52
  • 86