2

I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.

but in my case of 5 edit text its not working properly.

Please Help, Thanks.

developer
  • 423
  • 1
  • 4
  • 12
  • Post your code snippet and screen shot of the view if possible. Also have look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – udit7395 Mar 28 '18 at 08:03

2 Answers2

1

Try this

            button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String val1=editText1.getText().toString().trim();
            String val2=editText2.getText().toString().trim();
            String val3=editText3.getText().toString().trim();
            String val4=editText4.getText().toString().trim();
            String val5=editText5.getText().toString().trim();

            if(TextUtils.isEmpty(val5)){
                editText5.requestFocus();
                editText1.setFocusable(false);
                editText2.setFocusable(false);
                editText3.setFocusable(false);
                editText4.setFocusable(false);
            }else {
                editText4.setFocusable(true);
            }

            if(TextUtils.isEmpty(val4)){
                editText4.requestFocus();
                editText1.setFocusable(false);
                editText2.setFocusable(false);
                editText3.setFocusable(false);

            }else {
                editText3.setFocusable(true);
                editText1.setFocusable(false);
                editText2.setFocusable(false);
                editText4.setFocusable(false);
            }

            if(TextUtils.isEmpty(val3)){
                editText3.requestFocus();
                editText1.setFocusable(false);
                editText3.setFocusable(false);
                editText4.setFocusable(false);
            }else {
                editText2.setFocusable(true);
            }

            if(TextUtils.isEmpty(val2)){
                editText2.requestFocus();
            }else {
                editText1.setFocusable(true);
            }

            if(TextUtils.isEmpty(val1)){
                editText1.requestFocus();
            }

            if(!TextUtils.isEmpty(val1) &&
                    !TextUtils.isEmpty(val2) &&
                    !TextUtils.isEmpty(val3) &&
                    !TextUtils.isEmpty(val4) &&
                    !TextUtils.isEmpty(val5) ){
                Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
            }
        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thank you for answer it is working but I don't want user to write in next edit texts until previous edit texts validations are not complete. – developer Mar 28 '18 at 09:10
  • 1
    Thank you so much its working I'll update it according to my requirements. – developer Mar 28 '18 at 09:56
0
  • First check EditText empty,null or matches to some specific text based on your requirement.
  • Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which you want to do.
  • In else part, set things you want to set if, if or else if conditions false.

According to my understanding in your question, I wrote a code. try it.

final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);

if (et1.getText().toString().length() < 1) { //check EditText empty or not.
            //do what you want to edit/change in EditText. like below.
            et1.setFocusableInTouchMode(true);
            et1.requestFocus();

        } else if (et2.getText().toString().length() < 1) {
        //Check for second EditText empty or not. Do this for all 5 edit texts.
            et2.setFocusableInTouchMode(true);
            et2.requestFocus();
        }
        else{
        //if All EditTexts not empty. do what you want to edit/change in edittext.
            et1.setFocusableInTouchMode(false);
            et2.setFocusableInTouchMode(false);
        }
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
  • Thanks for your answer, Let me explain my question again, according to your solution's fields if et1 is empty then the focus of et1 should not be changed to et2. i.e. it should not allow me to write in et2 until et1 's validation are not completed. – developer Mar 28 '18 at 09:09