12

How to detect when a user copies a data and paste it in the edittext of the application. Only need to detect the paste event.

For example: When a user copies the credit card details from a saved note in the phone and paste it in the corresponding edittext of application, how can we detect it, only the paste event?

Or is there any other solution is available for to solve this?

Anu Roy
  • 369
  • 1
  • 2
  • 11
  • check this link http://stackoverflow.com/questions/10593724/paste-option-for-edittext – Nandakishore Shetty Jul 28 '15 at 05:35
  • You could add TextChangedListener to edittext and on onTextChanged event of TextWatcher you could check the size of CharSequence is greater than 1 Normaly this event is raised on both type and paste event type will consists of single char and paste consists of multiple char. – Muthu Jul 28 '15 at 05:53
  • For your solution, can you visit this http://stackoverflow.com/questions/24697236/android-how-to-detect-copy-event-of-edittext-in-android – Durgesh Patel Jul 28 '15 at 06:29
  • 2
    Possible duplicate of [Android intercept paste\copy\cut on editText](http://stackoverflow.com/questions/14980227/android-intercept-paste-copy-cut-on-edittext) – mikesterlingw Sep 01 '16 at 20:12

1 Answers1

14

You can set Listener Class:

public interface GoEditTextListener {
void onUpdate();
}

Сreate self class for EditText:

public class GoEditText extends EditText
{
    ArrayList<GoEditTextListener> listeners;

    public GoEditText(Context context)
    {
        super(context);
        listeners = new ArrayList<>();
    }

    public GoEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        listeners = new ArrayList<>();
    }

    public GoEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        listeners = new ArrayList<>();
    }

    public void addListener(GoEditTextListener listener) {
        try {
            listeners.add(listener);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /**
     * Here you can catch paste, copy and cut events
     */
    @Override
    public boolean onTextContextMenuItem(int id) {
        boolean consumed = super.onTextContextMenuItem(id);
        switch (id){
            case android.R.id.cut:
                onTextCut();
                break;
            case android.R.id.paste:
                onTextPaste();
                break;
            case android.R.id.copy:
                onTextCopy();
        }
        return consumed;
    }

    public void onTextCut(){
    }

    public void onTextCopy(){
    }

    /**
     * adding listener for Paste for example
     */
    public void onTextPaste(){
        for (GoEditTextListener listener : listeners) {
            listener.onUpdate();
        }
    }
}

xml:

<com.yourname.project.GoEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/editText1"/>

And in your activity:

private GoEditText editText1;

editText1 = (GoEditText) findViewById(R.id.editText1);

            editText1.addListener(new GoEditTextListener() {
                @Override
                public void onUpdate() {
//here do what you want when text Pasted
                }
            });
fHate
  • 195
  • 3
  • 9
  • 2
    How can u check the contents of whats about to be pasted? – Jono Sep 18 '18 at 14:55
  • @jonney you can put value to listener event, or just get it from EditText in onUpdate event – fHate Oct 02 '18 at 12:16
  • 7
    FYI This solution only works when user long presses edittext and then presses the paste popup menu. But if the device keyboard shows the copied text to the user in the suggestion box and allows the user to tap it to paste the text, then the above solution won't work. – Shivam Pokhriyal Nov 23 '20 at 14:14