package com.alexfu.formvalidatordemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alexfu.formvalidator.ValidationResult;
import com.alexfu.formvalidator.Validator;
import com.alexfu.formvalidator.rules.EmailRule;
import com.alexfu.formvalidator.rules.MinLengthRule;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements Validator.Callback {
    private Button validateButton;
    private EditText firstNameInput, lastNameInput, emailInput;
    private Validator validator = new Validator();
    private List<ValidationResult> validationErrors = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpViews();
        setUpValidator();
    }

    private void setUpValidator() {
        validator.setCallback(this);
        validator.addRule(firstNameInput, new MinLengthRule(1, "First name cannot be empty."));
        validator.addRule(lastNameInput, new MinLengthRule(1, "Last name cannot be empty."));
        validator.addRule(emailInput, new EmailRule("Invalid email."));

        // Form validation

        validateButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                validationErrors.clear();
                validator.validate();
            }
        });

        // Input validation

        validateOnTextChange(firstNameInput, lastNameInput, emailInput);
    }

    @Override public void onFieldValidated(ValidationResult result) {
        if (result.isValid()) {
            result.view.setError(null);
        } else {
            result.view.setError(result.errors.get(0));
            validationErrors.add(result);
        }
    }

    @Override public void onFormValidated() {
        if (validationErrors.isEmpty()) {
            Toast.makeText(this, "Form is valid!", Toast.LENGTH_SHORT).show();
        } else {
            validationErrors.get(0).view.requestFocus();
        }
    }

    private void validateOnTextChange(TextView... inputs) {
        for (final TextView input : inputs) {
            input.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // Ignore
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // Ignore
                }

                @Override public void afterTextChanged(Editable s) {
                    validator.validate(input);
                }
            });
        }
    }

    private void setUpViews() {
        firstNameInput = (EditText) findViewById(R.id.first_name_input);
        lastNameInput = (EditText) findViewById(R.id.last_name_input);
        emailInput = (EditText) findViewById(R.id.email_input);
        validateButton = (Button) findViewById(R.id.button_validate);
    }
}

@@ -111,71 +111,8 @@
 ty;%0A
-import android.text.Editable;%0Aimport android.text.TextWatcher;%0A
 impo
@@ -129,24 +129,24 @@
 .view.View;%0A
+
 import andro
@@ -199,40 +199,8 @@
 xt;%0A
-import android.widget.TextView;%0A
 impo
@@ -1598,111 +1598,8 @@
  %7D);
-%0A%0A        // Input validation%0A%0A        validateOnTextChange(firstNameInput, lastNameInput, emailInput);
 %0A   
@@ -2052,32 +2052,32 @@
        %7D else %7B%0A
+
             vali
@@ -2138,683 +2138,8 @@
  %7D%0A%0A
-    private void validateOnTextChange(TextView... inputs) %7B%0A        for (final TextView input : inputs) %7B%0A            input.addTextChangedListener(new TextWatcher() %7B%0A                @Override%0A                public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0A                    // Ignore%0A                %7D%0A%0A                @Override%0A                public void onTextChanged(CharSequence s, int start, int before, int count) %7B%0A                    // Ignore%0A                %7D%0A%0A                @Override public void afterTextChanged(Editable s) %7B%0A                    validator.validate(input);%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A
     

Remove validation on input change from demo.