Last active
May 13, 2024 05:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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 | |
} | |
}); | |
... |
This is awesome, thanks
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
and how can I get the copied or paste text ?