On focus out Keyboard hiding issue : after hiding keyBoard when I click in EditText it crashed

I am working on to hide keyboard when click out side the EditText. 
At first I set OnTouch listender & Click listener out site EditText elements.
When those elements touched I am calling a custom method (hideKeyBoard() show below).


private void hideKeyBoard() {
     InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

 }

and additionally I have to add those competent few attribute like  

android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"

to make those elements clickable & focusable. keyBoard was hiding & when I 
click EditText again I get error :

Caused by: java.lang.NullPointerException:Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference

solution : 

after implementing the following method the issue resolved. 
I can click back EditText again after hide keyBoard.

public void  hideKeyBoardEdt(EditText edt) {
    InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.
            INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
}


Comments

  1. The problem happens when there is no element focused, si it is enough with this validation:


    if(activity.getCurrentFocus() != null)
    {
    InputMethodManager inputMethodManager =
    (InputMethodManager) activity.getSystemService(
    Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(
    activity.getCurrentFocus().getWindowToken(), 0);
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Date picker dialog setMinDate(long) and setMaxDate(long) issue

How to calculate total number days difference between to dates in Android - java, Android

How to solve Android Studio rendering problem