Disable submit button when EditText is empty when using imeOption = actionSend

When the text is empty, we want to disable the button (how it is installed in portrait mode) Any ideas?

Edit: I don't think this is clear, but I can enable / disable my own button. But when using landscape mode, when the keyboard appears, the screen is covered with a special text area of ​​the android with its own button (hence imeOption). Therefore, I have no problem turning on / off the button that I have. This is an Android button that I want to disable when the text area is empty.

+4
source share
1 answer

Add a TextChangedListener that will be called every time the text inside the EditText changes.

 message.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) {} public void afterTextChanged(Editable s) { if (s == null || s.length() == 0) { send.setEnabled(false); message.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); } else { send.setEnabled(true); message.setImeOptions( /* whatever you previously had */ ); } } 

In addition, you can also let your class implement the TextWatcher interface, which makes the code cleaner.

 public class MyDialogFragment implements TextWatcher { ... } 
+12
source

Source: https://habr.com/ru/post/1382912/


All Articles