Android Question How to avoid Paste Clipboard menu on edittext_click?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I have an edit text that has been set to always hide the keyboard with Java code:

B4X:
Sub Globals()
 Private NativeMe As JavaObject
End Sub

#IF JAVA
import android.widget.EditText;
import android.os.Build;
    import android.text.InputType;
 public void disableSoftKeyboard(final EditText v) {
if (Build.VERSION.SDK_INT >= 11) {
v.setRawInputType(InputType.TYPE_CLASS_TEXT);
v.setTextIsSelectable(true);
v.setShowSoftInputOnFocus(false);
} else {
v.setRawInputType(InputType.TYPE_NULL);
v.setFocusable(true);
}
}
 public void enableSoftKeyboard(final EditText v) {
if (Build.VERSION.SDK_INT >= 11) {
v.setRawInputType(InputType.TYPE_CLASS_TEXT);
v.setTextIsSelectable(true);
v.setShowSoftInputOnFocus(true);
} else {
v.setRawInputType(InputType.TYPE_CLASS_TEXT);
v.setFocusable(true);
}
}
#END IF
Sub SetEditTextInput_Type(bDefaultKeyboard As Boolean, oEditText As  EditText)

If NativeMe.IsInitialized = False Then
NativeMe.InitializeContext
End If

If bDefaultKeyboard Then
NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText))
oEditText.InputType = 131217 'no suggestions
Else
NativeMe.RunMethod("disableSoftKeyboard", Array As Object(oEditText))
End If

End Sub
[CODE]

This all works fine, but I would like to avoid the Paste Clipboard menu to popup.
Any idea how this could be done?

RBS
 

Biswajit

Active Member
Licensed User
Longtime User
If you don't want the user to be able to alter (by typing/pasting) the value of edit text just disable it.

If you just want to hide the soft keyboard but allow the user to copy the content then just set the input type to none from the designer or from code and use this code
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
If you don't want the user to be able to alter (by typing/pasting) the value of edit text just disable it.

If you just want to hide the soft keyboard but allow the user to copy the content then just set the input type to none from the designer or from code and use this code

I did have a look at that link you mentioned, but I don't want to alter the popup that happens when clicking the cursor droplet.
All I want is that when you click the empty edittext the white Paste Clipboard dialog won't popup.
It is mainly relevant for a password edittext.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Did you try adding a handler for the long click event?

Yes, I did, but probably not the way you would do this.
This is the code. It doesn't show all the code, but it will give you the idea.
Works well for me.

B4X:
Sub KeyboardButton_LongClick

Dim b As Button = Sender
Dim iTag As Int
Dim iSelStart As Int
Dim iSelEnd As Int
Dim oEditText As EditText
Dim iKeyboardButton_Click_Time As Long

'suggestions not useful here
'---------------------------
If b.Tag = "btnSuggestion1" Or b.Tag = "btnSuggestion2" Then Return

iTag = b.Tag

'Log("KeyboardButton_LongClick, oEditText.Tag: " & oEditText.Tag)

Select Case General.iCurrentEditFocus
Case Enums.eFocussedEdit.DialogInput
oEditText = Dialog.edtInput
Case Enums.eFocussedEdit.SQL
oEditText = edtSQL
End Select

bLongKeyboardKeyClick = True

'this will be set to False by clicking another or the same key. That key won't then do anything else
'---------------------------------------------------------------------------------------------------
Do While bLongKeyboardKeyClick

'iCustomKBRepeatDelay can be set at Settings page 2
'--------------------------------------------------
  If DateTime.Now - iKeyboardButton_Click_Time > iCustomKBRepeatDelay Then
   If IntInIntArray(iTag, Array As Int(cKB.eSpecialKeyboardButtons.iCaps, _
cKB.eSpecialKeyboardButtons.iDel, _
cKB.eSpecialKeyboardButtons.iReturn, _
cKB.eSpecialKeyboardButtons.iSymbols)) = -1 Then 'not one of the non-character buttons
iSelStart = General.getSelectionStart(oEditText)
iSelEnd = General.getSelectionEnd(oEditText)
oEditText.Text = Insert(oEditText.Text, cKB.GetChar(iTag) , iSelStart, iSelEnd)
oEditText.SelectionStart = iSelStart + 1
If cKB.iKeyboardState = Enums.eKeyboardStates.UpperCase Then
cKB.SetKeyboardState(Enums.eKeyboardStates.LowerCase)
End If
Else
Select Case iTag
Case cKB.eSpecialKeyboardButtons.iCaps
Select Case cKB.iKeyboardState
Case Enums.eKeyboardStates.LowerCase
cKB.SetKeyboardState(Enums.eKeyboardStates.UpperCase)
b.TextColor = Colors.Black
Case Enums.eKeyboardStates.UpperCase
cKB.SetKeyboardState(Enums.eKeyboardStates.UpperCaseFixed)
b.TextColor = Colors.Blue
Case Enums.eKeyboardStates.UpperCaseFixed
cKB.SetKeyboardState(Enums.eKeyboardStates.LowerCase)
b.TextColor = Colors.Black
Case Enums.eKeyboardStates.Symbols1
cKB.SetKeyboardState(Enums.eKeyboardStates.Symbols2)
b.TextColor = Colors.Black
Case Enums.eKeyboardStates.Symbols2
cKB.SetKeyboardState(Enums.eKeyboardStates.Symbols1)
b.TextColor = Colors.Black
End Select
Case cKB.eSpecialKeyboardButtons.iDel
iSelEnd = General.getSelectionEnd(oEditText)
If cKB.bDeleteSQLWords Then
iSelStart = GetEndPreviousSQLWord(iSelEnd)
'iSelStart = iSelEnd - GetCurrentSQLWord.Length
Else
iSelStart = General.getSelectionStart(oEditText)
End If
oEditText.Text = Delete(oEditText.Text, iSelStart, iSelEnd)
If iSelStart = iSelEnd And iSelStart > 0 Then
oEditText.SelectionStart = iSelStart - 1
Else
If cKB.bDeleteSQLWords Then
If iSelStart > 0 Then
oEditText.SelectionStart = iSelStart - 1 '<<<
Else
oEditText.SelectionStart = 0
End If
Else
oEditText.SelectionStart = iSelStart
End If
End If
If iSelStart = 0 Then
bLongKeyboardKeyClick = False
End If
Case cKB.eSpecialKeyboardButtons.iSymbols
Select Case cKB.iKeyboardState
Case Enums.eKeyboardStates.LowerCase, Enums.eKeyboardStates.UpperCase, Enums.eKeyboardStates.UpperCaseFixed
cKB.SetKeyboardState(Enums.eKeyboardStates.Symbols1)
Case Enums.eKeyboardStates.Symbols1, Enums.eKeyboardStates.Symbols2
Select Case cKB.iCaps
Case Enums.eCaps.iNone
cKB.SetKeyboardState(Enums.eKeyboardStates.LowerCase)
Case Enums.eCaps.iTemp
cKB.SetKeyboardState(Enums.eKeyboardStates.UpperCase)
Case Enums.eCaps.iFixed
cKB.SetKeyboardState(Enums.eKeyboardStates.UpperCaseFixed)
End Select
End Select
Case cKB.eSpecialKeyboardButtons.iReturn
iSelStart = General.getSelectionStart(oEditText)
iSelEnd = General.getSelectionEnd(oEditText)
oEditText.Text = Insert(oEditText.Text, CRLF , iSelStart, iSelEnd)
oEditText.SelectionStart = iSelStart + 1
End Select
End If
iKeyboardButton_Click_Time = DateTime.Now
Sleep(0) 'this is needed otherwise this loop freezes!
  End If
 Loop

End Sub

RBS
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
I think you cant disable the context menu for every device. Some manufacturers use there own custom code to create the context menu. For a quick hack, you can clear the clipboard on edittext click/focus event.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I think you cant disable the context menu for every device. Some manufacturers use there own custom code to create the context menu. For a quick hack, you can clear the clipboard on edittext click/focus event.

> For a quick hack, you can clear the clipboard on edittext click/focus event.
I tried that with the Clipboard class (old library from 2011). It has the .clrText method, but it doesn't clear the clipboard
Had a search for clearing the clipboard, but there doesn't seem to be an easy way.

RBS
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
I have tested this. Its working.
B4X:
Dim jo As JavaObject
Dim ph As Phone
If ph.SdkVersion > 27 Then
    jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("clearPrimaryClip",Null)
Else
    jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("setText",Array As Object(""))
End If
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I have tested this. Its working.
B4X:
Dim jo As JavaObject
Dim ph As Phone
If ph.SdkVersion > 27 Then
    jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("clearPrimaryClip",Null)
Else
    jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("setText",Array As Object(""))
End If

I tried your code, but it doesn't clear the clipboard here on a Samsung S10.

RBS
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
Run this after clearing the clipboard code. Check the log.
B4X:
Log(jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("getPrimaryClip",Null))
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Run this after clearing the clipboard code. Check the log.
B4X:
Log(jo.InitializeContext.RunMethodJO("getSystemService",Array As Object("clipboard")).RunMethod("getPrimaryClip",Null))

The log did show Null and that is what I expected. The problem is that on this phone there seems to be 16 clipboard items. I cleared them all manually
and then I don't see the Paste Clipboard dialog, but instead I see an Autofill dialog. So your code is not sufficient to clear all the clipboard items.

RBS
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
The log did show Null and that is what I expected. The problem is that on this phone there seems to be 16 clipboard items. I cleared them all manually
and then I don't see the Paste Clipboard dialog, but instead I see an Autofill dialog. So your code is not sufficient to clear all the clipboard items.

RBS
That is all what android clipboard manager can do. Check the documentation.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I think you misunderstand, by adding a handler I meant something like:
B4X:
Private Sub EditText1_LongClick
' discard
End Sub
IIRC, this works for me for suppressing the copy/paste dialog.

OK, I got you, but I still need the long_click event for repeated key actions, mainly the Del key.
Will check though if that stops my Paste/Clipboard dialog.

RBS
 
Upvote 0
Top