I want to use this callback as a method parameter in my class.
In order to avoid duplication at the moment I'm just declaring it in a local variable but I'm not sure this is a best practice.
What's the best way to achieve this?
private val callback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
mode?.menuInflater?.inflate(R.menu.items_contextual_action_bar, menu)
isInActionMode = true
return true
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.action_delete -> {
DeleteItemsDialogFragment().show(childFragmentManager, DeleteItemsDialogFragment.TAG.toString())
true
}
else -> false
}
}
override fun onDestroyActionMode(mode: ActionMode?) {
isInActionMode = false
tracker?.clearSelection()
actionMode = null
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean = false
}
Related
I want hide option Paste when click double or Long Click Edittext
enter image description here
I tried:
edtSetName.customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(p0: ActionMode?, p1: Menu?): Boolean {
return true
}
override fun onPrepareActionMode(p0: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onActionItemClicked(p0: ActionMode?, p1: MenuItem?): Boolean {
return false
}
override fun onDestroyActionMode(p0: ActionMode?) {}
}
But that hide all option
I don't find a way to hide the menu popup, But you can disable it from pasting if the user taps on the menu
Create a custom EditText and override the onTextContextMenuItem method and return false for android.R.id.paste and android.R.id.pasteAsPlainText menu id's
#Override
public boolean onTextContextMenuItem(int id) {
switch (id){
case android.R.id.paste:
case android.R.id.pasteAsPlainText:
return false;
}
return super.onTextContextMenuItem(id);
}
On text selection, I do not want the user to see the Call option when they try to select a number from the TextView.
Is there a way to disable the call intent itself for the app?
I tried below, but I could not find the call option in the Menu to remove it.
fun TextView.disableCall() {
setCustomSelectionActionModeCallback(
object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?) = false
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
menu?.apply {
//Could not find the call option menu to remove
removeItem(android.R.id.copy)
removeItem(android.R.id.cut)
}
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?) = false
override fun onDestroyActionMode(mode: ActionMode?) {
// no-op
}
}
)
}
I hope you are well.
My question is why this callback class has two of the same functions?
the only difference that I see is that some functions deal with nullable and the others don't, but in the end, we can achieve what want with both ways (with nullable or without nullable)
if anyone has a good explanation for this and thanks in advance
private val actionModeCallback = object : Callback, android.view.ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
return false
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return false // Return false if nothing is done
}
// Called when the user selects a contextual menu item
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return false
}
// Called when the user exits the action mode
override fun onDestroyActionMode(mode: ActionMode) {
actionMode = null
}
override fun onCreateActionMode(p0: android.view.ActionMode?, p1: Menu?): Boolean {
// Called when the action mode is created; startActionMode() was called
// Inflate a menu resource providing context menu items
val inflater: MenuInflater = p0!!.menuInflater
inflater.inflate(R.menu.memory_fragment_context_menu, p1)
return true
}
override fun onPrepareActionMode(p0: android.view.ActionMode?, p1: Menu?): Boolean {
return false
}
override fun onActionItemClicked(p0: android.view.ActionMode?, p1: MenuItem?): Boolean {
return when (p1?.itemId) {
R.id.someId -> {
Toast.makeText(context,"go to hell", Toast.LENGTH_LONG).show()
selectionTracker?.clearSelection()
p0?.finish() // Action picked, so close the CAB
true
}
else -> false
}
}
override fun onDestroyActionMode(p0: android.view.ActionMode?) {
}
}
I am struggling with removing PASTE option. Although i am able to remove PASTE option from menu while selecting text from edtittext but in some devices instead of Paste, Clipboard option is visible. Can anyone suggest any workaround ?
#Override
public int getSelectionStart() {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
Log.e("TAG","====="+ element.getMethodName()+ "====");
if (element.getMethodName().equals("canPaste") || element.equals("canSuggest")) {
return -1;
}
}
return super.getSelectionStart();
}
You can remove all types of copy paste from EditText by using this kotlin extension function:
fun EditText.disableCopyPaste() {
this.customInsertionActionModeCallback= object : ActionMode.Callback{
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
this.customSelectionActionModeCallback = object : ActionMode.Callback{
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
this.isLongClickable = false
this.setTextIsSelectable(false)
this.addTextChangedListener( object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
if (after - count > 1) {
this#disableCopyPaste.setText(s)
this#disableCopyPaste.setSelection(s.toString().length)
}
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
}
})
}
Link reference: https://gist.github.com/SanikaVanjari/83758a53c9d52012e034e855c4f8653e
I am trying setup contextual action bar when long click on a item in a recyclerview. But menu icon is not shown.
Code is given below.
listAdapter.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(position: Int) {
...
}
override fun onItemLongClick(position: Int): Boolean {
if (actionMode != null) {
return false
}
val activity = activity as AppCompatActivity
actionMode = activity.startSupportActionMode(actionModeCallback)
return true
}
})
Action Mode callback
internal val actionModeCallback = object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.item_delete -> {
actionMode?.finish()
return true
}
}
return false
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
actionMode?.menuInflater?.inflate(R.menu.menu_context, menu)
actionMode?.title = "delete"
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
actionMode = null
}
}
menu_context.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item_delete"
android:title="#string/menu_option_text_delete"
android:icon="#drawable/ic_delete_white_24dp"
app:showAsAction="always"
/>
</menu>
What I tried?
using non support lib version for Activity and ActionMode
tried changing action mode background to see whether the issue with the theming
set SHOW_AS_ACTION_ALWAYS in onPrepareActionMode
Did not work, even the action mode title is not shown.
I am using androidx, appCompat version : 1.1.0-rc01
Please help!
The soulution is to update the menus manually in onPrepareActionMode
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.findItem(R.id.menu_archive).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_upload_to_cloud).setShowAsAction
(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Actually this was a mistake in my part as pointed out in the issue tracker
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
/*-->*/mode?.menuInflater?.inflate(R.menu.menu_context, menu)
return true
}
Note that you should use the passed in mode and not your own
ActionMode object to get the correct inflater.