Question

I have an activity that is showing an EditText and two bottoms.

When I tap on the EditText the Android Virtual Keyboard shows up so that I can type in my text. Now, before tapping in any bottom I would like to hide the keyboard. I would like to do it by tapping on the screen.

I have seen posts here in stackoverflow with some similar question but that does not look like working. I have tried to set a listener:

   // Create an anonymous implementation of OnFocusChangeListener
   private OnFocusChangeListener mFocusListener = new OnFocusChangeListener() {
       public void onFocusChange(View v, boolean b) {
          // do something when the focus changes
        hideSoftKeyboard(v);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setupUI(findViewById(R.id.parent));
        EditText editText = (EditText) findViewById (R.id.edit_message);
        editText.setOnFocusChangeListener(mFocusListener);
        setContentView(R.layout.activity_main);
    }

I have also tried to create a parent activity that recursively associate an onTouch event to every view that is not a text View but it does register only the text view (I took this piece of code from another stackoverflow post)

    public void setupUI(View view) {

    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(v);
                return false;
            }

        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            setupUI(innerView);
        }
    }
}

Any straitgh forward solution for this? I can not believe that there is not a more simple way to do this. I am using Gingerbread API (API Level 10)

Thanks

Was it helpful?

Solution

Ok, I found a way to do this in a very simple way: XML Layout definition. Since a Layout is a ViewGroup we can implement events on it. Go and define the method to handle a click on the layout (hideSoftKeyboard)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:id="@+id/main_layout" 
    android:onClick="hideSoftKeyboard" >

And here is how I implemented the method:

public void hideSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

OTHER TIPS

I used the following method for the same purpose

private void hideKeypad(){
    EditText edtView=(EditText)findViewById(R.id.username);

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edtView.getWindowToken(), 0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top