EditText: Differentiate between text change by setText() or by keyboard input

There are already a lot of good workarounds here! I wanted to add what worked for me to give options to whoever might be having this issue in the future.

I used the TextWatcher and simply relied on checking for what element currently has focus when the EditText is being edited. Note that this would work if in your app, the user has to give focus to the EditText (by clicking on it for instance) before entering a text, and you're sure that another element will have the focus when you're using setText in your code.

Something like this

yourEditText.addTextChangedListener(
            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) {
                    if (yourEditText.hasFocus) {
                        //this is a user input
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            }
    );

I finally solved the problem by implementing a InputConnectionWrapper (see this question and particularly the answer for an implementation example) which has various methods to get the input from a soft-keyboard. I return my InputConnectionWrapper in the method EditText.onCreateInputConnection(). For hard keyboards I use EditText.onPreIme(). All these methods are overwritten and route their input through my framework which handles the text input and updates the View accordingly. This means, in all these overwritten methods (except for onCreateInputConnection()) the super method is not called cause I update the View myself. This prevents inconsistencies between my data model and the View.


I believe android do not let you to distinguash between programmatically and manually entered text. The only workaround fo you as to use some sla that will indicate when test was set by your code, because you are always know when you call setText().