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);
}
The problem happens when there is no element focused, si it is enough with this validation:
ReplyDeleteif(activity.getCurrentFocus() != null)
{
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}