I want to stop a user from copying text or images from my screen to any text editor.
Also I want to prevent taking screen shots of activities.
How can I do it programmatically in Android?
In terms of screenshots - try using FLAG_SECURE - as the docs state
Window flag: treat the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays.
You can use it in an Activity as follows:
public class NonScreenshotableActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Place this before setting layout but after calling super method
getWindow().setFlags(LayoutParams.FLAG_SECURE,
LayoutParams.FLAG_SECURE);
setContentView(R.layout.my_layout);
//Rest of your activity code here
}
}
You can also stop any EditText fields from being copy/pasteable by using the following:
yourEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
You can see the docs for ActionMode.Callback() for more on this but it essentially aborts loading the copy/paste dialog
Ed George answers the first part!
Here is the Second half
If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.
edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).
Related
In my activity, I have an action mode defined as follows.
#Override
public void onShowPlace(final String placeId, final String placeName) {
Log.i("PLACE", placeId);
actionModeFeedByLocation = startSupportActionMode(new ActionMode.Callback(){
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(placeName);
om.slideHorizontal(R.id.overlay_nf_bylocation_fragment_container,
NewsfeedFragment.newInstance(om), TAG_NFBYLOCATION_FRAGMENT);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Log.i("DESTROY_ACTIONMODE", "TEST");
om.back();
}
});
}
This renders a newsfeed for a single place in an overlay fragment.
From this feed, I want to show the detail in yet a second overlay by creating another action mode:
#Override
public void onShowUser(final User user) {
actionModeUserProfile = startSupportActionMode(new ActionMode.Callback(){
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(user.getFbName());
om.slideHorizontal(R.id.overlay_nf_userprofile_fragment_container,
UserProfileFragment.newInstance(user.getFbId(), user.getFbName(), user.getGender()), TAG_USERPROFILE_FRAGMENT);
Log.i("ACTION_MODE", String.valueOf(actionModeFeedByLocation == actionModeUserProfile));
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
om.back();
}
});
}
Unfortunately, this doesn't work; the call to the detail of the user destroys the first action mode.
I use action mode to maintain consistent "back" behaviour.
As illustration, this is what I need to achieve visually:
It is important to note that:
These fragment appear as overlays on top of each other (should not be destroyed unless "back" is called on them, so I created containers for each in the activity xml)
The "home" (first) screen has set a toolbar to the SupportActionBar
What I need is the back button (in the header + device button) to behave consistently on each overlay, and remove each later correctly.
I solved the problem by overriding onBackPressedand onOptionsItemSelected, and manually syncing the toolbars title / managing the fragment backstack through custom code in the aforementioned callbacks.
I have an login fragment. For password (Edittext), I need to disable the Paste and select ALL options.
I tried setCustomSelectionActionModeCallback and "set longClickable " but they are not working.
Any suggestions Thanks in advance
edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).
This will definitely work
All you need to do is add mode.finish() in onCreateActionMode()
mEntryText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
public void onDestroyActionMode(ActionMode mode) { }
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
/* This is done to solve the issue of SELECT ALL PASTE etc., showing up.
mode.finish() should not be called at this point. If called, it will throw
this exception "E/DecorView[]: Destroying unexpected ActionMode instance of TYPE_FLOATING;
com.android.internal.view.FloatingActionMode#2e022bc was not the current floating action mode! Expected null"
This makes that class not to execute the rest of the code and hence the FloatingAction is never created!
I wish the the author of FloatingActionMode class gave me a method to just stop showing it. menu.clear()
is supposed to do it, but doesn't do.
*/
mode.finish(); // menu.close(); menu.clear();
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
});
I am working in xamarin forms to create an app for android. I want to disable the copy/paste functionality of textbox in android. I used the following line to disable it
Control.LongClickable = false;
But its working only in case of if user press the text for long time. But if user click multiple times on text, he becomes able to copy paste. How I can completely disable the copy paste functionality of textbox?
In eclipse this can be used, Just check will it be helpful.
textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
// after onCreateView and findById you you_editText view;
you_editText.CustomSelectionActionModeCallback = new CopyPasteDisabler();
class CopyPasteDisabler : Java.Lang.Object, Android.Views.ActionMode.ICallback
{
public bool OnActionItemClicked(Android.Views.ActionMode mode, IMenuItem item) => false;
public bool OnCreateActionMode(Android.Views.ActionMode mode, IMenu menu) => false;
public void OnDestroyActionMode(Android.Views.ActionMode mode) {}
public bool OnPrepareActionMode(Android.Views.ActionMode mode, IMenu menu) => false;
}
I have problem with edit text while copy/paste. I know there are lots of question about this on stack-overflow,
the problem is,
editText editText1 = (EditText)findViewById(R.id.editText1);
editText1.setLongClickable(false);
editText1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
i added the code above, but i couldn't prevent android to show paste icon as you can see below link,
image link
https://www.dropbox.com/s/8r3th4bcg4co6fu/screenshot_2013-12-10-14-34-24.png
any feedback will be appreciated,
thanks...
Set longClickable false for edittext on xml file
android:longClickable="false"
In HTC device with OS 2.x we want to disable the cut copy paste option on the edit text.
How can we disable this?
Help Appreciated.
Thanks!
Bhushan
Try Below code:
edtUserName.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});