developer
developer

Reputation: 423

Do not change focus if edit text is empty

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.

Upvotes: 2

Views: 1836

Answers (2)

AskNilesh
AskNilesh

Reputation: 69689

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();
            }
        }
    });

Upvotes: 1

Dinith Rukshan Kumara
Dinith Rukshan Kumara

Reputation: 680

  • 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);
        }

Upvotes: 0

Related Questions