Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Button once all EditText Fields are not empty

I have nine EditText fields that i am using TextWatcher, what i am hoping to get working is if All nine EditText fields are filled enable the Button if not Disable,

I have the Button disabled in OnCreate so when the program first runs the button is Disabled but once if i enter a number into one Edit Text field the button becomes enabled, even if the other eight fields are empty, also once i remove all Text from all fields the button is still enabled.

EditText T1, T2, T3, T4, T5, T6, T7, T8, T9;
//    Button buttonClear;
Button ButtonScore;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    T1 = (EditText) findViewById(R.id.editText2);
    T2 = (EditText) findViewById(R.id.editText3);
    T3 = (EditText) findViewById(R.id.editText4);
    T4 = (EditText) findViewById(R.id.editText5);
    T5 = (EditText) findViewById(R.id.editText6);
    T6 = (EditText) findViewById(R.id.editText7);
    T7 = (EditText) findViewById(R.id.editText8);
    T8 = (EditText) findViewById(R.id.editText9);
    T9 = (EditText) findViewById(R.id.editText10);
    ButtonScore = (Button) findViewById(R.id.buttonScore);
    ButtonScore.setEnabled(false);

    T1.addTextChangedListener(watcher);
    T2.addTextChangedListener(watcher);
    T3.addTextChangedListener(watcher);
    T4.addTextChangedListener(watcher);
    T5.addTextChangedListener(watcher);
    T6.addTextChangedListener(watcher);
    T7.addTextChangedListener(watcher);
    T8.addTextChangedListener(watcher);
    T9.addTextChangedListener(watcher);

    //updateButtonScore();
}

private final TextWatcher watcher = 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)
    {}
    @Override
    public void afterTextChanged(Editable s) {
        if (T1.getText().toString().length() == 0 && T2.getText().toString().length() == 0 &&
                T3.getText().toString().length() == 0 && T4.getText().toString().length() == 0 &&
                T5.toString().trim().length() == 0 && T6.getText().toString().length() == 0 &&
                T7.getText().toString().length() == 0 && T8.getText().toString().length() == 0 &&
                T9.getText().toString().length() == 0) {
            ButtonScore.setEnabled(false);
        } else {
            ButtonScore.setEnabled(true);
        }
    }
};
like image 913
David Avatar asked Oct 04 '14 20:10

David


1 Answers

"Your logic is flawed"

Since you want to disable the button if any one of the EditText-fields are empty, you should check them with OR (||) instead of AND (&&). If you use AND, the button will only be disabled if all the EditTexts are empty at the same time.

You also have a bug in your code: T5.toString().trim().length().
T5.toString() is a textual representation of the EditText, and should never be empty, this coupled with the usage of AND will lead to the condition never beeing true.

like image 75
Jave Avatar answered Sep 27 '22 06:09

Jave