tags:

views:

42

answers:

0

Below is a bit of code that I managed to get to work to hide the soft keyboard on android. It works by the user clicking anywhere on the screen (outside of a EditText input/s) to hide the IME soft keyboard. It registers a OnTouchListener to the ScrollView (id="@+id/sv_background) which when the screen is touched it hides the IME soft keyboard via the InputMethodManager code. I have set the scroll view as the parent layout in this case but it also works with any other layout view.

I hope this is useful to someone out there in Android land.

XML

<ScrollView 
android:id="@+id/sv_background" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"&gt;

   <other views and EditTexts/>

</ScrollView>

Java

private ScrollView svBackground;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newentry);

    svBackground = (ScrollView)findViewById(R.id.sv_background);
    svBackground.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(svBackground.getWindowToken(), 0);
            return false;
        }
    });
}