Android Question Edittext: remove the display of the context menu

mberthe

Member
Licensed User
Longtime User
I used the solution given by Erel:

B4X:
Sub ContextMenu_Create(AM As JavaObject, Fw As View)
    ActionMode=AM
    FocusedView=Fw
    Dim menu As JavaObject = ActionMode.RunMethod("getMenu", Null)
    menu.RunMethod("clear", Null)
End Sub

#if Java
 
 import android.view.*;
 import anywheresoftware.b4a.AbsObjectWrapper;
 import android.text.Selection;
 import android.widget.EditText;

    @Override
    public void onActionModeStarted(ActionMode mode) {
        processBA.raiseEvent(this, "contextmenu_create", AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4j.object.JavaObject(), mode),
            AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.ConcreteViewWrapper(), getCurrentFocus()));
        super.onActionModeStarted(mode);
    }

    @Override
    public void onActionModeFinished(ActionMode mode) {
        super.onActionModeFinished(mode);
    }
    public static int getSelectionLength(EditText et) {
        return Selection.getSelectionEnd(et.getText()) - Selection.getSelectionStart(et.getText());
    }

#End If

For Android 4 devices the "context menu" is not changed but it doesn't matter.

For Android >= 5 it works except for the menu item "select all"
 

Jorge M A

Well-Known Member
Licensed User
Longtime User
That tutorial is to create a "Custom Context Menu".
If you want to make the context menu disappear completely, try with this code:
B4X:
Sub DisableContextMenu(eT As EditText)
    Dim jo As JavaObject = eT
    Dim e As Object = jo.CreateEvent("android.view.ActionMode.Callback", "Common.Action", False)
    jo.RunMethod("setCustomSelectionActionModeCallback", Array As Object(e))
End Sub

Usage:
B4X:
DisableContextMenu(txtPassword)
 
Upvote 0

mberthe

Member
Licensed User
Longtime User
That tutorial is to create a "Custom Context Menu".
If you want to make the context menu disappear completely, try with this code:
B4X:
Sub DisableContextMenu(eT As EditText)
    Dim jo As JavaObject = eT
    Dim e As Object = jo.CreateEvent("android.view.ActionMode.Callback", "Common.Action", False)
    jo.RunMethod("setCustomSelectionActionModeCallback", Array As Object(e))
End Sub

Usage:
B4X:
DisableContextMenu(txtPassword)

Same result:
For Android 4 devices the "context menu" is not changed

For Android >= 5 it works except for the menu item "Select all"
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Longtime User
Please upload your sample project. Here is working fine in Android 4.4.2 , 5.0.1, 7 and 9.
Are you disabling after load layout?
 
Upvote 0

mberthe

Member
Licensed User
Longtime User
Please upload your sample project. Here is working fine in Android 4.4.2 , 5.0.1, 7 and 9.
Are you disabling after load layout?

With an Android 4.4.2 device, it displays the item "paste"
With an Android 7.0 device it displays the item "Select all"
 

Attachments

  • essai_CM.zip
    9 KB · Views: 225
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Longtime User
With an Android 4.4.2 device, it displays the item "paste"
With an Android 7.0 device it displays the item "Select all"
I confirm the described behavior.
It may be possible to disable these options with some "inline java", which is not my domain.

I found this on Stackoverflow, but it needs someone other to help implement in B4A

Here is a hack to disable "paste" popup. You have to override EditText method:

@Override
public int getSelectionStart() {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
if (element.getMethodName().equals("canPaste")) {
return -1;
}
}
return super.getSelectionStart();
}
This solution works on newer versions of Android as well, unlike the accepted answer.
Source: https://stackoverflow.com/questions...u-pop-up-on-text-selection-handler-click-even (see 3rd answer)
 
Upvote 0

mberthe

Member
Licensed User
Longtime User
i found this solution :
B4X:
dim edittext1 as edittext
dim ref1 as reflector
'. . .
ref1.Target=Edittext1   
ref1.SetOnLongClickListener("context_longclick")
'. . .
Sub context_longclick(viewtag As JavaObject) As Boolean
    Return True
End Sub
 
Upvote 0
Top