1

I have an edittext an it have many words in it. After I paste it some keywords and I want to catch only pasted keywords.

I find different solution but its only gave me text is pasted or cut. I want keywords too.

How to detect the paste event in editext of the application?

Is there any solution like swift or Do we have to hack again.

And sorry for my terrible english.

"Test Test Test" -> paste new -> "Test Test Pasted Text Here Test"

I want to get "Pasted Text Here"

6155031
  • 4,171
  • 6
  • 27
  • 56

1 Answers1

2

You have to make a custom EditText that can catch paste event. For that, it will need to override onTextContextMenuItem(int id)

Something like that is enough :

public class MyEditText extends EditText {

    UpdateListener listener;

    public MyEditText(Context context) {
        super(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setUpdateListener(UpdateListener listener){
        this.listener = listener;
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        boolean consumed = super.onTextContextMenuItem(id);

        switch (id){
            case android.R.id.cut :
                if(listener != null) listener.onCut();
                break;
            case android.R.id.copy :
                if(listener != null) listener.onCopy();
                break;
            case android.R.id.paste :
                if(listener != null) listener.onPaste();
        }

        return consumed;
    }

    interface UpdateListener{
        void onCut();
        void onCopy();
        void onPaste();
    }
}

Then, in activity, you have to implement the interface given by this custom EditText

editText = findViewById(R.id.textview2);
editText.setUpdateListener(new MyEditText.UpdateListener() {
    @Override
    public void onCut() {
        Log.i(TAG, "onCut: ");
    }

    @Override
    public void onCopy() {
        Log.i(TAG, "onCopy: ");
    }

    @Override
    public void onPaste() {
        Log.i(TAG, "onPaste: ");
        // triggered when code is pasted
    }
});

If i understand well you want that when the user paste somethinig, it erases what was written and paste the text. In onPaste() :

@Override
public void onPaste() {
    Log.i(TAG, "onPaste: ");
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    String pasteData = "";
    //check if something present in clipboard, and check if it is text
    if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) {
        ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
        pasteData = item.getText().toString();
        editText.setText(pasteData);
    }
}
Kilarn123
  • 723
  • 5
  • 6