Android Question Avoid softkeyboard popping up on Activity_Resume

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I have my own custom keyboard and that works all fine, except when the app is paused with an edittext focussed then on Activity_Resume the default softkeyboard will popup.
I can hide it in IME_HeightChanged with pho.HideKeyboard(Activity) but that is not ideal as you will see the default keyboard appearing first and then disappearing.
Is there a way to avoid this in the first place?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I have my own custom keyboard and that works all fine, except when the app is paused with an edittext focussed then on Activity_Resume the default softkeyboard will popup.
I can hide it in IME_HeightChanged with pho.HideKeyboard(Activity) but that is not ideal as you will see the default keyboard appearing first and then disappearing.
Is there a way to avoid this in the first place?

RBS

I forgot to say that all the edittexts have the softkeyboard disabled with Java code (thanks to Jeffrey Cameron:
)
I disabled the focussed edit text again in Active_Resume, but still the softkeyboard pops up.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Try this (manifest editor):
B4X:
SetActivityAttribute(Main,  android:windowSoftInputMode, stateAlwaysHidden)

Thanks, that works nicely.
In my app I can switch between the default softkeyboard and the custom keyboard,
so how could I achieve the same in code, outside the manifest editor?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You can use conditional compilation in the manifest editor. You asked about changing it in your code. The generated manifest is static. It cannot be modified at runtime.

Yes, thanks, I understand it now.
Any idea why the Java code (as shown in the mentioned posting) works on normal startup, but not after Activity_Resume?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You haven't posted enough information.

All solved now.
I put together the very simplest project showing how to disable the softkeyboard in code:

B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub Globals
    Private edtTest As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    DisableSoftKeyboard(edtTest)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

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, 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

Sub DisableSoftKeyboard(oEditText As EditText)
    
    If NativeMe.IsInitialized = False Then
        NativeMe.InitializeContext
    End If
    
    NativeMe.RunMethod("disableSoftKeyboard", Array As Object(oEditText))
    
End Sub

Now this works fine, the softkeyboard doesn't show and also when putting the focus in the edit text
and pausing the app and then resuming the app the keyboard doesn't show, so the Java code does
the job nicely. Why then in the real app this was not so and why then did the soft keyboard still show
in the above scenario on resuming? Comparing to the simple example above showed the answer as the
only difference was that NativeMe in the simple example was declared in Sub Process_Globals and the real
app in Sub Globals and changing that solved the problem, so a simple mistake indeed.
So now I don't need in the manifest anymore:

B4X:
SetActivityAttribute(Main,  android:windowSoftInputMode, stateAlwaysHidden)

And I am able to switch between the default softkeyboard and my custom softkeyboard in code, so all solved indeed.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
All solved now.
I put together the very simplest project showing how to disable the softkeyboard in code:

B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub Globals
    Private edtTest As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    DisableSoftKeyboard(edtTest)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

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, 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

Sub DisableSoftKeyboard(oEditText As EditText)
   
    If NativeMe.IsInitialized = False Then
        NativeMe.InitializeContext
    End If
   
    NativeMe.RunMethod("disableSoftKeyboard", Array As Object(oEditText))
   
End Sub

Now this works fine, the softkeyboard doesn't show and also when putting the focus in the edit text
and pausing the app and then resuming the app the keyboard doesn't show, so the Java code does
the job nicely. Why then in the real app this was not so and why then did the soft keyboard still show
in the above scenario on resuming? Comparing to the simple example above showed the answer as the
only difference was that NativeMe in the simple example was declared in Sub Process_Globals and the real
app in Sub Globals and changing that solved the problem, so a simple mistake indeed.
So now I don't need in the manifest anymore:

B4X:
SetActivityAttribute(Main,  android:windowSoftInputMode, stateAlwaysHidden)

And I am able to switch between the default softkeyboard and my custom softkeyboard in code, so all solved indeed.

RBS

Actually, it seems the problem of the default softkeyboard popping up on Activity_Resume even when in custom keyboard mode
was to do with having this in the manifest tile:

B4X:
SetActivityAttribute(Main, android:windowSoftInputMode, adjustResize)

Instead of this:

B4X:
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)

Now it works all as required, both in default softkeyboard mode and custom softkeyboard mode.

RBS
 
Upvote 0
Top