For use dispatchTouchEvent()
in DialogFragment, override onCreateDialog
and return a custom Dialog
with dispatchTouchEvent
(in your custom DialogFragment).
Exemple, for dismiss keyboard when click outside in DialogFragment:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()) {
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent motionEvent) {
if (getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(motionEvent);
}
};
}
dispatchTouchEvent()
on theActivity
from theDialog
'sdispatchTouchEvent()
. However implementing this turned a little nasty for me since I found that this does not work forListView
andGridView
'sOnItemClickListener
i.e. you don't get any callbacks (Only on some devices). All the other views and layouts worked fine though! – Wingding