Skip to content

Instantly share code, notes, and snippets.

@guillermomuntaner
Last active May 13, 2024 05:44
Show Gist options
  • Save guillermomuntaner/82491cbf0c88dec560a5 to your computer and use it in GitHub Desktop.
Save guillermomuntaner/82491cbf0c88dec560a5 to your computer and use it in GitHub Desktop.
EditText which notifies of Cut, Copy and Paste events via an attachable listener
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Original:
* An EditText, which notifies when something was cut/copied/pasted inside it.
* @author Lukas Knuth
* @version 1.0
*
* Update:
* Added a OnCutCopyPasteListener so this class can be used as a plug&play component
* @author Guillermo Muntaner on 14/01/16.
*
* Source & discussion:
* https://stackoverflow.com/questions/14980227/android-intercept-paste-copy-cut-on-edittext
*/
public class CutCopyPasteEditText extends EditText {
public interface OnCutCopyPasteListener {
void onCut();
void onCopy();
void onPaste();
}
private OnCutCopyPasteListener mOnCutCopyPasteListener;
/**
* Set a OnCutCopyPasteListener.
* @param listener
*/
public void setOnCutCopyPasteListener(OnCutCopyPasteListener listener) {
mOnCutCopyPasteListener = listener;
}
/*
Just the constructors to create a new EditText...
*/
public CutCopyPasteEditText(Context context) {
super(context);
}
public CutCopyPasteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CutCopyPasteEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* <p>This is where the "magic" happens.</p>
* <p>The menu used to cut/copy/paste is a normal ContextMenu, which allows us to
* overwrite the consuming method and react on the different events.</p>
* @see <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#TextView.onTextContextMenuItem%28int%29">Original Implementation</a>
*/
@Override
public boolean onTextContextMenuItem(int id) {
// Do your thing:
boolean consumed = super.onTextContextMenuItem(id);
// React:
switch (id){
case android.R.id.cut:
onCut();
break;
case android.R.id.copy:
onCopy();
break;
case android.R.id.paste:
onPaste();
}
return consumed;
}
/**
* Text was cut from this EditText.
*/
public void onCut(){
if(mOnCutCopyPasteListener!=null)
mOnCutCopyPasteListener.onCut();
}
/**
* Text was copied from this EditText.
*/
public void onCopy(){
if(mOnCutCopyPasteListener!=null)
mOnCutCopyPasteListener.onCopy();
}
/**
* Text was pasted into the EditText.
*/
public void onPaste(){
if(mOnCutCopyPasteListener!=null)
mOnCutCopyPasteListener.onPaste();
}
}
...
CutCopyPasteEditText editText = (CutCopyPasteEditText) findViewById(R.id.your_et);
editText.setOnCutCopyPasteListener(new CutCopyPasteEditText.OnCutCopyPasteListener() {
@Override
public void onCut() {
// Do your onCut reactions
}
@Override
public void onCopy() {
// Do your onCopy reactions
}
@Override
public void onPaste() {
// Do your onPaste reactions
}
});
...
@mikemike396
Copy link

Works flawlessly! Thank you!

@LuisFVargas
Copy link

LuisFVargas commented Oct 31, 2019

and how can I get the copied or paste text ?

@jimybrian
Copy link

This is awesome, thanks

@dramirezm
Copy link

dramirezm commented Feb 13, 2020

and how can I get the copied or paste text ?

Using ClipboardManager class , you can get the last text you copied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment