Try replacing onClick with onTouch . To do this, you need to change the layout attributes as follows:
<RelativeLayout android:id="@+id/relativeLayout" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true"> <RelativeLayout> // widgets here </RelativeLayout> </RelativeLayout>
Then remove the rl_main_onClick(View view) {...} method and insert the onTouch method inside onCreate() :
findViewById(R.id.relativeLayout).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); return true; } });
Marat source share