Android Question Disable soft keyboard in b4xlib

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Experimenting with B4XPreferencesDialog.b4xlib and using this in a B4XPages project.
In that B4XPages project I use a custom keyboard, so the normal default keyboard is disable with this code, which is in the main module.

B4X:
#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 static void enableSoftKeyboard(final EditText v, int iKeyBoardType) {
    if (Build.VERSION.SDK_INT >= 11) {
        v.setRawInputType(iKeyBoardType);
        v.setTextIsSelectable(true);
        v.setShowSoftInputOnFocus(true);
     } else {
        v.setRawInputType(iKeyBoardType);
        v.setFocusable(true);
     }
                     
}
#END IF

This is called from B4XMainPage with this code:

B4X:
Sub Class_Globals
    Public NativeMe As JavaObject
End Sub

Sub SetEditTextInput_Type(bDoCustomKeyboard As Boolean, oEditText As  Object, iKeyBoardType As Int, bHandleAction As Boolean)
    
    'Log("SetEditTextInput_Type, iKeyBoardType: " & iKeyBoardType)
    
    If NativeMe.IsInitialized = False Then
        NativeMe.InitializeContext
    End If
    
    If bDoCustomKeyboard Then
        NativeMe.RunMethod("disableSoftKeyboard", Array As Object(oEditText))
        
        Log("SetEditTextInput_Type")
        oEditText.InputType = Bit.Or(oEditText.InputType, 0x00080000)   '0x00080000 = 524288, this doesn't stop the Auto-fill popup if the edit text has no text yet
    Else
        Select Case iKeyBoardType
            Case Enums.eKeyboardStates.LowerCase, Enums.eKeyboardStates.UpperCase, Enums.eKeyboardStates.UpperCaseFixed, _
                 Enums.eKeyboardStates.Symbols1, Enums.eKeyboardStates.Symbols2
                NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText, (1 + 524288)))
            Case Enums.eKeyboardStates.Numeric
                NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText, 2))
                ime.SetCustomFilter(oEditText, oEditText.INPUT_TYPE_NUMBERS, "0123456789")
            Case Enums.eKeyboardStates.Decimals
                NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText, (2 + 8192)))
                ime.SetCustomFilter(oEditText, oEditText.INPUT_TYPE_NUMBERS, "0123456789.")
            Case Enums.eKeyboardStates.NumericWithMinus
                NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText, (2 + 4096)))
                ime.SetCustomFilter(oEditText, oEditText.INPUT_TYPE_NUMBERS, "0123456789-")
            Case Enums.eKeyboardStates.DecimalsWithMinus
                NativeMe.RunMethod("enableSoftKeyboard", Array As Object(oEditText, (2 + 4096 + 8192)))
                ime.SetCustomFilter(oEditText, oEditText.INPUT_TYPE_NUMBERS, "0123456789.-")
        End Select
        
        If bHandleAction Then
            'will show Next button to handle the action
            'will need to specify the action in ime_HandleAction
            ime.AddHandleActionEvent(oEditText)
        End If
    End If

End Sub

All this works fine, but not sure where to put the Java code in a b4xlib.
I think the only place it would work in the B4XPages project was in Main, but a b4xlib doesn't have such module.

RBS
 
Reactions: byz

aeric

Expert
Licensed User
Longtime User
Why not create a sub?
B4X:
Sub DisableSoftKeyboard (oEditText As Object)
    NativeMe.InitializeContext
    NativeMe.RunMethod("disableSoftKeyboard", Array As Object(oEditText))
End Sub

Put the Java code in the same class.
 
Reactions: byz
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thanks, got this working now as a b4xlibrary with the default keyboard disabled.

RBS
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…