4

Currently I have two edit-text,suppose I want to make validation for empty edittext check.What is better way for runtime validation.

My code is;

    final EditText ev1 = (EditText) findViewById(R.id.editText1);
    final EditText ev2 = (EditText) findViewById(R.id.editText2);


    ev1.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View rv, boolean hasFocus) {
    if(!hasFocus && ev1.getText().length()==0)
    {    
        ev1.requestFocus();
        ev2.clearFocus();
    }
    }
});

In this onclick of second editText it clears the focus of second EditText when First Edittext is empty,but keyboard entries always done in Second Text,and we can never get focus in First Text.

Please don't suggest different different focus listener for different EditText as this editText may be added dynamically too.

Having Focus in Both Text!!!!

More CLEARLY:Just simple thing to validate ev1 before losing focus,not allow other views to get focus until any character is entered in it.

Hanry
  • 5,481
  • 2
  • 40
  • 53

4 Answers4

10
    final EditText ev1 = (EditText) findViewById(R.id.editText1);
    final EditText ev2 = (EditText) findViewById(R.id.editText2);

    ev1.setOnFocusChangeListener(new OnFocusChangeListener() {      
        @Override
        public void onFocusChange(View rv, boolean hasFocus) {
            if(!hasFocus && ev1.getText().length()==0)
            {    
                  new Handler().postDelayed(new Runnable() {

                  @Override
                  public void run() {
                       ev2.clearFocus();
                   ev1.requestFocus();

                  }
            }, 100);

         }
      }
    });
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Chirag Nagariya
  • 1,864
  • 1
  • 11
  • 6
0

Try this :

EDIT :

        final EditText ev1,ev2,ev3,ev4,ev5,ev6;

        ev1 = (EditText) findViewById(R.id.editText1);
        ev2 = (EditText) findViewById(R.id.editText2);
        ev3 = (EditText) findViewById(R.id.editText3);
        ev4 = (EditText) findViewById(R.id.editText4);
        ev5 = (EditText) findViewById(R.id.editText5);
        ev6 = (EditText) findViewById(R.id.editText6);

        setValidateAction(ev2);
        setValidateAction(ev3);
        setValidateAction(ev4);
        setValidateAction(ev5);
        setValidateAction(ev6);

Declare this method :

public void setValidateAction(final EditText edit_action) {
        edit_action.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus && ev1.getText().length()==0){    
                    ev1.requestFocus();               
                }
            }
        });
}            

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • for this you need to add focusListener to every next editText listener,also it is not fix that user will go to next edittext only may go to other edittext 3/4/ and so on.. when multiple editTexts are there. – Hanry Jan 11 '13 at 07:31
  • @hanry you want generic validation. Like user can only get focus on 2nd,3rd,4th... edittext if and only if user has entered any value for edittext 1. Is it so? – Pratik Sharma Jan 11 '13 at 07:35
  • @hanry see the edited solution as per your requirement. This will validate for all the `edittext` you are using. – Pratik Sharma Jan 11 '13 at 07:52
0

I have taken on xml having two textboxes and tried this way to solve your issue.

check the below code and tell me is it ok for you?

    setContentView(R.layout.activity_main);
    final EditText password = (EditText)findViewById(R.id.editText1);
    final EditText confirmpassword = (EditText)findViewById(R.id.editText2);

    password.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(password.getText().toString().trim().length()==0)
            {
                confirmpassword.setText("");
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    confirmpassword.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus && password.getText().toString().trim().length()==0)
            {
                // First fill password
                password.requestFocus();
            }
        }
    });
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0
 add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (isEmpty(name)) {
                    return;
                }
            }
    });


public boolean isEmpty(EditText... editTexts)
    {
        boolean res=true;
        for (EditText editText:editTexts) {
            if (TextUtils.isEmpty(editText.getText())) {
                editText.setError("Fill field");
                editText.requestFocus();

                res = true;
                break;
            }
            else {
                res = false;
            }
        }
        return res;
    }
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Feb 19 '21 at 14:36