Whenever I have text in an EditText, the user can do a longpress over the text and the "Copy/Cut/Share" options hover over it.
It is OK for me to let Copy/Cut (Paste also) options appear (although not really needed), but I really can't let those text trigger the "share" option appear.
Is this feasible without reinventing the wheel ?
The XML code to define the EditText is quite basic, currently testing against API 30:
<EditText
android:id="#+id/settings_doctor_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/dr_name"
android:inputType="textCapCharacters"
android:singleLine="true"
android:textSize="26sp" />
Having had a review on EditText and TextView documentation, but I still haven't found any idea on do to deal with this.
Did I miss anything in the docs ? Thanks for reading !
I share with you the solution derived from your comments that has been implemented and tested:
I created a my own TextEdit:
public class EditText extends android.support.v7.widget.AppCompatEditText {
public EditText(Context context) {
super(context);
this.setCustomSelectionActionModeCallback(removeShareActionMode);
}
ActionMode.Callback removeShareActionMode = new ActionMode.Callback() {
#Override
#TargetApi(26)
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (menu != null) {
menu.removeItem(android.R.id.shareText);
}
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
}
And used it everywhere. Works as expected. Thanks to all for contributing. I'm sorry I don't have enough reputation to vote your answers.
Related
Actually I want to hide ActionPopupWindow (popup having SELECT ALL, CLIPBOARD options) when user click on + icon(refer to the attached image).
ActionPopupWindow appears when user click on the Text Selection Handler(bubble) (which appears when user tap on the text in the EditText).
I have tried to use setTextIsSelectable() method of EditText but it is not working consistently.
Any help or guidance will be well appreciated.
UPDATE: To hide the Popup already opened and showing on the screen, you need to clear focus of the current EditText or focus on other view when you clicked the plus button. See the
example below:
iconPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yourEditText.clearFocus();
}
});
If you want the popup never shows up at the first place, there are many ways to do it.
The simplest way is disabling long click and selection feature:
yourEditText.setLongClickable(false);
yourEditText.setTextIsSelectable(false);
Second one is overriding action callback actions on your edittext:
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 use them separately or together according to your case.
Also you can check other options from here
I want to disable copy paste in all of my android page and no one cant long touch on my application .
android:longClickable="false" it dosent work.
This worked for me:
In xml, disable the long-click in the EditText:
android:longClickable="false".
Also, you have to return false from these methods:
mEditEext.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;
}
});
I think it will work fine. User can not able to copy your page to another app.
//Inside onResume and onDestroy
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("");
Here, It will restrict for background process "Secondary app". But you can copy and paste it only inside your app.
I have set CustomSelectionActionModeCallback to an edittext as below
new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#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) {
}
};
to disable paste option on edittext,but still user can paste by tapping on the cursor .I tried to create custom edittext class and override canPaste(),but still no luck.Can anybody suggest a correct solution.
Thanks.
Apparently the problem is observed in lollipop+ devices only
Another method one would suggest is to call setLongClickable(false) on the EditText, although that still doesn't prevent pasting from tapping on the cursor.
The proper (or only :) way to do this is to create a custom EditText and override isSuggestionsEnabled and canPaste methods.
Check this answer for an example.
this is my app strategy:
there is a text view that contains texts.
for editing them, i define Action mode items(e.g. EDIT- this will
texts of text view into edit text)although there are some default
actions like select all/copy/.. as selecting text, so i don not want
to define them.
i use this code for enabling the default selecting/copy buttons.
but as i clicked the buttons, nothing happens. why???
xml code:
<TextView
android:id="#+id/speech"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:textSize="25sp" android:textAlignment="inherit"
android:selectAllOnFocus="true"
android:background="#drawable/curved_background"
android:layout_centerInParent="true" android:textIsSelectable="true"
android:padding="10dp" />
initializing TextView in class:
speech_text = (TextView) findViewById(R.id.speech);
registerForContextMenu(speech_text);
speech_text.setTextIsSelectable(true);
speech_text.setCustomSelectionActionModeCallback(new SelectText());
SelectingText java class:
public class SelectText implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.text_select, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Log.d(Log, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
switch (item.getItemId()) {
case ... //other actions
return true;
}
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
Note:
as i control my code. the problem is in defining SelectText class in this line: speech_text.setCustomSelectionActionModeCallback(new SelectText()); because as i delete thiscode, every thing worksgood!
You can make your text in app enable for copy/paste in clipboard by adding below line :
android:textIsSelectable
For more information, Please check below reference,
http://developer.android.com/reference/android/widget/TextView.html
Thanks.
This should work for you.
In your xml jus make sure that....
android:focusable="true"
android:textIsSelectable="true"
In my activity, I have an action mode with a single item which has a title and an icon.
I want both the title and the icon to be displayed, so I use SHOW_AS_ACTION_WITH_TEXT and SHOW_AS_ACTION_ALWAYS flags.
In landscape orientation, it's fine. I have title + icon.
But in portrait orientation only the icon is displayed (though there's a lot of free space). Does anyone know what I could do to fix it ?
Note that the title is correctly displayed if I remove the icon.
Here is my sample code:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActionMode(new Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add("Item 1").setIcon(R.drawable.ic_launcher).setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
#Override
public void onDestroyActionMode(ActionMode mode) { }
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
});
}
}
Does anyone know what I could do to fix it ?
I doubt that you can. "Always" and "with text" are requests, not commands. The framework does not always honor either of them.
Instead of adding this menu item via code try to use this attribute:
android:showAsAction="always|withText"
and add it to the item in the menu.xml