How to disable one edittext when we start typing other editext in android?

65 Views Asked by At

I have a form with two editext and condition is only one should be enable when we enter value to any of edittext e.g We have 2 edittexts ...Edittext1 and Editext2 case 1: When we type edittext1 then editext2 should be disable and greyout. Case 2 : when we type editext2 the editext1 should be disable and greyout. enter image description here

I had tried using textwatcher but couldn't find the logic.

TextWatcher cupToDup = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String cupTOdup = et_cup_to_dup.getText().toString().trim();
        et_dup_to_cup.setFocusable(false);
        //et_dup_to_cup.setText(" ");
        et_dup_to_cup.setEnabled(false);

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

TextWatcher dupTocup = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

        String dupTocup = et_dup_to_cup.getText().toString().trim();
        et_cup_to_dup.setFocusable(false);
        //et_cup_to_dup.setText(" ");
        et_cup_to_dup.setEnabled(false);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
1

There are 1 best solutions below

0
MKE SIRIES II On

I found the solution like this,

  et_dup_to_cup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    et_cup_to_dup.setText("");
                    et_dup_to_cup.setAlpha(1);
                    et_cup_to_dup.setAlpha(0.25f);
                   
                }
            }
        });

        et_cup_to_dup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    et_dup_to_cup.setText("");
                    et_cup_to_dup.setAlpha(1);
                    et_dup_to_cup.setAlpha(0.25f);
                   
                }
            }
        });