Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private WebView1 As WebView
Dim FocusedView As View
Dim ActionMode As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
WebView1.LoadUrl("https://www.b4x.com")
End Sub
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)
Dim event As Object = menu.CreateEvent("android.view.MenuItem.OnMenuItemClickListener", "ContextMenuClick", True)
For Each s As String In Array("Set As Title", "Delete") 'menu items here
menu.RunMethodJO("add", Array(s)).RunMethod("setOnMenuItemClickListener", Array(event))
Next
End Sub
Sub ContextMenuClick_Event (MethodName As String, Args() As Object) as boolean
Dim MenuItem As JavaObject = Args(0)
Dim title As String = MenuItem.RunMethod("getTitle", Null)
Log(title & " was clicked!") '<---- do what ever you like with the click!!!
If FocusedView Is EditText Then
Dim et As EditText = FocusedView
Dim jo As JavaObject = Me
Dim selectionLength As Int = jo.RunMethod("getSelectionLength", Array(et)) 'will be available in the next version of B4A!
Dim selectionStart As Int = et.SelectionStart
Select title
Case "Set As Title"
Activity.Title = et.Text.SubString2(selectionStart, selectionLength + selectionStart)
Case "Delete"
et.Text = et.Text.SubString2(0, selectionStart) & _
et.Text.SubString(selectionStart + selectionLength)
End Select
End If
ActionMode.RunMethod("finish", Null)
Return True
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
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