android paste event

Buda Gavril picture Buda Gavril · Mar 2, 2011 · Viewed 10.2k times · Source

Is there a way to catch the paste event in my application? I must do something when I click long on an editText and select Paste from context menu. Thanks

Answer

Guildenstern70 picture Guildenstern70 · Sep 1, 2011

You should implement a TextWatcher listener on the control that receives the paste action.

The TextWatcher class provides methods to handle the OnChange, BeforeChange and AfterChange of any Editable. For instance:

private void pasteEventHandler() {
    ((EditText)findViewById(R.id.txtOutput))
            .addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                    Log.d(TAG, "Text changed, refreshing view.");
                    refreshView();
                }

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

                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {
                }
            });
}