Home » Android » Set addTextChangedListener in a function Android

Set addTextChangedListener in a function Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have 5 addTextChangedListener for 5 edit text .I want to customize them in a single function so that i have to just pass the id of the editttext and addTextChangedListener get applied on that.How could i do this i am not getthing this .Please help me on this .

Code

mobileNumber.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mobileNumber.setError(null);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                mobileNumber.setError(null);
            }
        });

        mobileNumber2.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mobileNumber2.setError(null);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                mobileNumber2.setError(null);
            }
        });

        mobileNumber1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mobileNumber1.setError(null);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                mobileNumber1.setError(null);
            }
        });

All addTextChangedListener are doing the same thing for the different id .So is this possible in android to customize it as a function and we have to just pass the id on that and addTextChangedListener will be applied on that

As suggest

public class BookingClassAddListenerOnTextChange implements TextWatcher {
    private Context mContext;
    EditText mEdittextview;

    public BookingClassAddListenerOnTextChange(Context context, EditText edittextview) {
        super();
        this.mContext = context;
        this.mEdittextview= edittextview;

    }
    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }
    public void addTextChangedListener(
            BookingClassAddListenerOnTextChange bookingClassAddListenerOnTextChange) {
        // TODO Auto-generated method stub

    }

}
How to&Answers:

You should create a Listener class like so, Just modify the parameters in the constructor
to accept the EditText ID you want to add a listener to.

public class addListenerOnTextChange implements TextWatcher {
    private Context mContext;
    EditTextView mEdittextview;

    public addListenerOnTextChange(Context context, EditTextView edittextview) {
        super();
        this.mContext = context;
        this.mEdittextview= edittextview;
    }

    @Override
    public void afterTextChanged(Editable s) {  
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //What you want to do
        }
    }
}

You call them like this in your activity

mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this, mobileNumber2));

Again modify the parameters as needed.

Answer:

If all the five behave in the same way, then you can create an object of TextWatcher and pass the same to all.

TextWatcher tw = new TextWatcher() {
    /* code goes here */
    /* viewOnFocus can be used here */
}

mobilenum1.addTextChangedListener(tw);
mobilenum2.addTextChangedListener(tw);
mobilenum3.addTextChangedListener(tw);

The viewOnFocus variable is set via an onFocusChange listener

OnFocusChangeListener of = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            viewInFocus = view;
        }
    }
};
mobile1.setOnFocusChangeListener(of);
mobile2.setOnFocusChangeListener(of);
mobile3.setOnFocusChangeListener(of);