4

When the text is empty, we want to disable the button (which is how it's set on portrait mode) Any ideas?

Edit: I don't think it's clear but I can enable/disable my own button.. But when using the landscape mode, when the keyboard pops up, the screen is covered by an android specific text area with it's own button (hence the imeOption) So I don't have a problem enabling/disabling the button I have.. It's this Android button that I want to disable when the text area is empty..

Tolga E
  • 12,188
  • 15
  • 49
  • 61
  • you can set visibility View.INVISIBLE and overide the onconfigurationChange set its state again if you need – Last Warrior Nov 24 '11 at 15:38
  • How do i get a handle on that Send button.. It's an OS rendered button.. I think that screen is outside of the scope of my activity.. – Tolga E Nov 24 '11 at 15:59
  • http://stackoverflow.com/questions/2979441/android-how-to-enable-my-button-back-if-edittext-is-not-empty – Synxmax Nov 24 '11 at 16:52
  • I don't have a problem enabling/disabling my own button.. This is the android rendered button.. – Tolga E Nov 24 '11 at 16:58

1 Answers1

12

Add a TextChangedListener which will get called whenever the text inside the EditText gets changed.

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 */ );
        }
}

Alternatively, you can also let your class implement the TextWatcher interface which makes the code a bit cleaner.

public class MyDialogFragment implements TextWatcher { ... }
jgillich
  • 71,459
  • 6
  • 57
  • 85
FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • Hey, for some reason this didn't work but it kinda lead me down the right path.. I saw where the action was taken for that send button and basically did nothing if the text field was empty – Tolga E Nov 25 '11 at 02:40
  • It is likely that you'd have to somehow trigger a refresh of the keyboard for the ime options to take affect (hide & show?). Another good tip is to call trim() (or use a regax that removes whitespace) on a temp copy of the string and then check the length - makes sure you don't send a message with just whitespace – FunkTheMonk Nov 25 '11 at 09:40
  • @FunkTheMonk As far as I understood your answer, I could use `afterTextChanged()` to show and hide a `DONE` button of a softkeyboard. Right? I also tried to disable/enable a `NEXT` button without success. How would you do this? – JJD Aug 13 '12 at 19:52
  • @JJD it has been awhile, as far as I remember you should be able to show and hide any of the enter actions (search, next, enter, etc). But I've seen devices / versions of manufactures UIs (sense, touchwiz, etc) which ignore the enter action flags - try on another device. You could also try hiding and reshowing the keyboard, or re-initialising it somehow - maybe InputMethodService.onConfigurationChanged using the current configuration (from Resources) – FunkTheMonk Aug 14 '12 at 07:20
  • @FunkTheMonk Thank you for the advice. I will delay implementing that feature I guess. The emulator show the `NEXT` and `DONE` buttons, however, the Samsung S1 does not. – JJD Aug 14 '12 at 21:14