-1

Here is my code hope it is clear enough With this code i am trying to use the isEnabled function to check if my EditText fields have values, if they don't have values the submit Button must be disabled else if they all have values the button must enable so the user can navigate to new activity

public class RegisterAccountActivity extends AppCompatActivity {

    //Declaring Button and EditText
    private Button submit;
    private EditText name;
    private EditText surname;
    private EditText email;
    private EditText phoneNumber;
    private EditText address;

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

        submit = (Button) findViewById(R.id.btnSubmitPersonalDetails);
        name = (EditText)findViewById(R.id.edtName);
        surname = (EditText)findViewById(R.id.edtSurname);
        email = (EditText)findViewById(R.id.edtEmail);
        phoneNumber = (EditText)findViewById(R.id.edtPhoneNumber);
        address = (EditText)[findViewById][1](R.id.edtPostalAddress);

        //Calling the checking fields method
        checkFields();

    }

    //Function to disable button if fields doesn't have value
    public void checkFields() {

       //Here i am checking if EditText have values
        if (!submit.getText().toString().isEmpty() &&
                !name.getText().toString().isEmpty() &&
                !surname.getText().toString().isEmpty() &&
                !email.getText().toString().isEmpty() &&
                !phoneNumber.getText().toString().isEmpty() &&
                !address.getText().toString().isEmpty()) {

            submit.setEnabled(true);
        } else {
            submit.setEnabled(false);
        }

    }

[Find attached image for the form]

enter image description here

Linh
  • 57,942
  • 23
  • 262
  • 279
Nhlanhla
  • 3
  • 1
  • 6

5 Answers5

0

Kickoff example for one EditText:

name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkFields();
            }
        });

Assign TextWatcher to each of your EditText

Divers
  • 9,531
  • 7
  • 45
  • 88
0
addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count)
    { 
     //set your condition here 
    }
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override 
    public void afterTextChanged(Editable s) {

    }  });
Shubham Jain
  • 2,365
  • 2
  • 17
  • 29
0

Use textWatcher:

private final TextWatcher passwordWatcher = 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) {
            //set enable or disable at here
        }
    };

You can process at the afterTextChanged method.

sonnv1368
  • 1,547
  • 12
  • 17
0

USE MULTIPLE TEXTWACHER FOR THAT LIKE BELOW

    TextWatcher watcher = new TextWatcher() {
  @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //YOUR CODE
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //YOUR CODE
        }

        @Override
        public void afterTextChanged(Editable editable) {
         if (editable.equals(name.getEditableText())) {
        // DO STH
            checkfields();
    } else if (editable.equals(phone.getEditableText())) {
        // DO STH
           checkfields();
    }
     like that/////////
        }
    };

Assign all the edittext in oncreate

 name.addTextChangedListener(watcher);
        email.addTextChangedListener(watcher);
        phone.addTextChangedListener(watcher);
        address.addTextChangedListener(watcher);

Hope this will help to you,let me know.

Andolasoft Inc
  • 1,296
  • 1
  • 7
  • 16
0
Direct used button clicked this code

subimit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             if (!submit.getText().toString().isEmpty() &&
            !name.getText().toString().isEmpty() &&
            !surname.getText().toString().isEmpty() &&
            !email.getText().toString().isEmpty() &&
            !phoneNumber.getText().toString().isEmpty() &&
            !address.getText().toString().isEmpty()) {

      //Hear direct intent pass new activity

    } else {

    }
        }
    });
mujjuraja
  • 359
  • 2
  • 11