you can encode the text which you select from views and copy to clipboard.
Then if you paste that code in any other apps you can only see the encoded string.
In your app you need to decode data before pasting on to your view
Here i will provide the code to support my statement:
public class MainActivity extends Activity {
EditText editText;
private ClipboardManager myClipboard;
private ClipData myClip;
String base64;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
editText = (EditText) findViewById(R.id.editText3);
editText.setCustomSelectionActionModeCallback(new Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.copy:
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText = editText.getText()
.subSequence(min, max);
String text = selectedText.toString();
byte[] data = null;
try {
data = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
base64 = Base64.encodeToString(data, Base64.DEFAULT);
myClip = ClipData.newPlainText("text", base64);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
// Finish and close the ActionMode
mode.finish();
return true;
case android.R.id.cut:
return true;
case android.R.id.paste:
int min2 = 0;
int max2 = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min2 = Math.max(0, Math.min(selStart, selEnd));
max2 = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText2 = editText.getText()
.subSequence(min2, max2);
String text2 = selectedText2.toString();
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item11 = abc.getItemAt(0);
String text1 = item11.getText().toString();
byte[] dataDecode = Base64.decode(text1, Base64.DEFAULT);
String textDecode = null;
try {
textDecode = new String(dataDecode, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editText.getText().replace(min2, max2, textDecode, 0,
textDecode.length());
Toast.makeText(getApplicationContext(), "Text Pasted",
Toast.LENGTH_SHORT).show();
// mode.finish();
return true;
default:
break;
}
return false;
}
});
}
}
Here am trying to override copy and paste methods which appear in action bar when the user long clicks the EditText.
My solution is an alternate way to hide data in other applications.
Paste pop up which appears when you click on text insertion handle and paste option which comes when you select some text and long clicks it, both are different from my observation.
In order to work with paste pop up, I created a custom EditText class and in that class I defined a method to do the required task. Here it is...
/**
* An EditText, which notifies when something was cut/copied/pasted inside it.
*/
public class MonitoringEditText extends EditText {
private final Context context;
private ClipboardManager myClipboard;
String base64;
EditText editText = (EditText) findViewById(R.id.editText3);
/*
* Just the constructors to create a new EditText...
*/
public MonitoringEditText(Context context) {
super(context);
this.context = context;
}
public MonitoringEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public MonitoringEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@Override
public boolean onTextContextMenuItem(int id) {
myClipboard = (ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
boolean consumed = super.onTextContextMenuItem(id);
switch (id) {
// case android.R.id.cut:
// onTextCut();
// break;
case android.R.id.paste: // here I am catching that pop up paste event
try {
onTextPaste();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
// case android.R.id.copy:
// onTextCopy();
//
}
return consumed;
}
// /**
// * Text was pasted into the EditText.
// * @throws UnsupportedEncodingException
// */
public void onTextPaste() throws UnsupportedEncodingException {
int min2 = 0;
int max2 = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min2 = Math.max(0, Math.min(selStart, selEnd));
max2 = Math.max(0, Math.max(selStart, selEnd));
}
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString(); //text is my encoded data
byte[] data = Base64.decode(text, Base64.DEFAULT);
String text1 = new String(data, "UTF-8"); //text1 is decoded data
editText.getText().replace(min2, max2, text1);
String fullText = editText.getText().toString();
editText.setText(fullText.replace(text, ""));
}
}