12

I have a custom view that could show a view in same space that should be soft keyboard native for android.

I need to having the keyboard opened, click in a button, hide the keyboard and shows other view in same place that keyboard be/was.

I have that implemented right now just with a hide keyboard and show custom view but has a weird behavior and min lag and overlapping.

Has someone implemented a similar stuff?

colymore
  • 9,604
  • 13
  • 45
  • 84

3 Answers3

3

I have checked the Github project and found the bug and I have fixed that bug with the following code:

if (isRedPanelVisible()) {
    showRedPanel(false);
    showKeyboard(true, new KeyboardCallback() {
        @Override
        public void onKeyboardDone(boolean isVisible) {

        }
    });
}
if (KeyboardVisibilityEvent.isKeyboardVisible(TestActivity.this)) {
    hideKeyboard(TestActivity.this);
    new android.os.Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            showRedPanel(true);
        }
    }, 100);

Note: You just have to put this in TestActivity.java under button's click event and Remove the previous code.

What I did

if your readPanel is visible then I called the showRedPanel to false and try to open the keyboard.

After that I have added a check for Keyboard's visibility event and if keyboard is visible I called hideKeyboard to make keyboard go away and call showReadPanel with true after a delay of 100 ms

Code: hideKeyboard

public void hideKeyboard(Activity activity) {
        // Check if no view has focus:
        try {
            View view = activity.getCurrentFocus();
            if (view != null) {
                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        } catch (Exception e) {

        }
    }
Rahul Khurana
  • 7,582
  • 5
  • 29
  • 54
  • Hi, thanks for your answer. However this solution only fixes the issue that keyboard and red panel are displayed at the same time. The other issue (when neither of them is displayed) is still present - and even more visible. I think, the key question is how to switch red panel and keyboard visibility in single layout/render pass so they are swapped seamlessly. – sidon Mar 08 '18 at 09:22
  • In that case you need to render [KeyboardView](https://developer.android.com/reference/android/inputmethodservice/KeyboardView.html) and redPanel layout in single layout container. – Rahul Khurana Mar 08 '18 at 09:41
3

So what happens in your code is that: Tell system to close the keyboard -> Show red panel with a small delay -> Red panel is shown before keyboard closing -> Since keyboard mode is in adjustResize the red panel shown above keyboard -> Keyboard get closed -> Everything in place

Try to change windowSoftInputMode in manifest from adjustResize to adjustNothing.

Sadly keyboard in android doesn't work smoothly like in IOS, keyboard is handled by OS means you no control over it size, opening/closing animation and no callback! So the best way is to always show red panel and when needed Open keyboard on top of it.

Keivan Esbati
  • 2,855
  • 1
  • 17
  • 35
-2

se the following functions to show/hide the keyboard:

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}
Ashish Patil
  • 364
  • 3
  • 20