Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to detect Copy event of Edittext in android

I have one android apps in that i want : whenever user press copy from the edittext,any event occure, it any of edittext like from messenger edittext,mail edittext any one,when user press copy text, i want to occure any event,so any body give me example for this ?i have no idea about it,so please help me, thanks in advance.

like image 326
Nirav Mehta Avatar asked Jul 11 '14 12:07

Nirav Mehta


2 Answers

I got solutions: I create one service : on that in oncreate :

         ClipboardManager clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
         clipBoard.addPrimaryClipChangedListener(new ClipboardListener());

and add in service :

        class ClipboardListener implements
        ClipboardManager.OnPrimaryClipChangedListener {
        public void onPrimaryClipChanged() {
        ClipboardManager clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        CharSequence pasteData = "";
        ClipData.Item item = clipBoard.getPrimaryClip().getItemAt(0);
        pasteData = item.getText();
        Toast.makeText(getApplicationContext(), "copied val=" + pasteData,
                Toast.LENGTH_SHORT).show();

    }
}
like image 142
Nirav Mehta Avatar answered Oct 26 '22 23:10

Nirav Mehta


By using the below code for EditText you can get the event for Cut/Copy/Paste.

public class EditTextMonitor extends EditText{
private final Context mcontext; // Just the constructors to create a new EditText...

public EditTextMonitor(Context context) {
    super(context);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mcontext = context;
}


@Override
public boolean onTextContextMenuItem(int id) {
    // Do your thing:
    boolean consumed = super.onTextContextMenuItem(id);
    // React:
    switch (id){
        case android.R.id.cut:
            onTextCut();
            break;
        case android.R.id.paste:
            onTextPaste();
            break;
        case android.R.id.copy:
            onTextCopy();
    }
    return consumed;
}

/**
 * Text was cut from this EditText.
 */
public void onTextCut(){Toast.makeText(mcontext, "Event of Cut!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was copied from this EditText.
 */
public void onTextCopy(){
    Toast.makeText(mcontext, "Event of Copy!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was pasted into the EditText.
 */
public void onTextPaste(){
    Toast.makeText(mcontext, "Event of Paste!", Toast.LENGTH_SHORT).show();
}}
like image 29
Manikanta Ottiprolu Avatar answered Oct 26 '22 23:10

Manikanta Ottiprolu