Home > database >  runOnUIThread for Android is hiding the soft keyboard
runOnUIThread for Android is hiding the soft keyboard

Time:01-06

In my android application, the core logic is written in C . Hence for multiple scenarios, there will be JNI calls happening.

In one use-case, as the text is being entered by the user in the 'EditText', I call a JNI method which will capitalize each word (the logic and hence the outcome can vary for case to case).

The TextChange handler calls a JNI function which will capitalize each word the text in a worker thread and then invoke a method on the main thread using 'runOnUIThread' to update the text field's value. My expectation is this should happen seamlessly and should not hamper the way user is interacting.

Code Samples:

Calling of the JNI function-

     edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                
                OnTextChanged (edittext.getId(), String.valueOf(s));
            }
            ...
     });

Updating the EditText with the capitalized text-

// Below function is invoked on the Main Thread once the Worker Thread is done with its job of capitalizing
// An entry function is called which gets the textfield object from the layout and then called the below
public static void UpdateTextField(EditText pEditTextObj, int pID, int pPosX, int pPosY, int pHeight, int pWidth, int pColor, String pText)
{
        Log.d("SampleJNIApp01", "TextField valued to be updated = "   pText);
        
        pEditTextObj.setText(pText);
        pEditTextObj.requestFocus();
        pEditTextObj.setCursorVisible(true);
        pEditTextObj.setSelection(pEditTextObj.getText().length());

        // TODO - Show the keyboard 
}

Observation :

Once the first letter is typed in, it DOES get capitalized by the JNI function and the text in the EditText is also updated. However, the focus goes away, cursor is not visible and the keyboard gets hidden.

Same is observed when I am typing in the text field and some worker thread has invoked some java method to 'runOnUIthread' - the focus is lost and soft keyboard gets hidden.

Is there something wrong that I am doing ?

CodePudding user response:

Not sure why runOnUIthread is making the keyboard to hide.

But you can try using Handler to post update to UI instead.

Handler(Looper.getMainLooper()).post {
//Update UI.
 }

CodePudding user response:

I think there are two options, you can use to prevent the keyboard from being hidden when posting on UI Thread:

first you can add this code (adjustPan) in your manifest XML file in holder activity(if you are in Fragment, add it to the activity which call the fragment):

<activity android:windowSoftInputMode="adjustPan"> </activity>

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

the second option is to use an explicit call of a soft keyboard after you post to the UI Thread :

InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  • Related