Tuesday, March 13, 2018

Android show view and hide keyboard at same time. Weird behaviour

Leave a Comment

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?

3 Answers

Answers 1

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) {          }     } 

Answers 2

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 are 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.

Answers 3

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); } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment