3

I have a customized Dialog. Inside the dialog, there's an EditText. When I touch inside the EditText, the soft keyboard is shown. I want to hide this keyboard when I touch inside the other place of the Dialog.

I know how to hide a keyboard in activity. I just don't know how to do this in Dialog.

Thank you.

Vigor
  • 1,706
  • 3
  • 26
  • 47
  • 1
    have you searched how to hide software keyboard? there are a lot of examples here. look here http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – Bruno Bieri Nov 04 '12 at 06:54
  • @viperbone hi, I have searched how to hide software keyboard in activity, use dispatchTouchEvent. but I just don't know how to do this in a dialog. – Vigor Nov 04 '12 at 13:54
  • oh, good. You could write that in your question so it's clear you search a specially a method to hide the keyboard within a dialog. – Bruno Bieri Nov 04 '12 at 18:11

2 Answers2

4

You can do that easy by using focuslisners, see my code sample below:

EditText.setOnFocusChangeListener(new View.OnFocusChangeListener() 
        { 
            @Override 
            public void onFocusChange(View v, boolean hasFocus) 
            { 
                if (hasFocus) 
                { 
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    imm.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT); 
                }
                else
                {
                   InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                   imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
                }
            } 
        });

edit:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
            android:id="@+id/wrapper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:focusable = "true"
            android:isFocusableInTouchMode = "true"
            android:clickable = "true" > 

                <EditText
                android:id="@+id/EditText"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:hint="hint"
                android:singleLine="true" />

            </LinearLayout>         
    </RelativeLayout>

\to be sure to get focus:

    LinearLayout actionHide = (LinearLayout) findViewById(R.id.wrapper);
        actionHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionHide.requestFocus(); // use this to trigger the focus listner
                //or use code below to set the keyboard to hidden
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);

            }
        });
Greezer
  • 515
  • 2
  • 4
  • 18
  • Hi, this code can't work in the dialog. I mean, when I touch the outside of the EditText, this EditText is still on focus. Thank you. Do you have other ideas? – Vigor Nov 04 '12 at 13:56
  • You need to set the dialog focusable, then it does work. (See my edit) – Greezer Nov 04 '12 at 15:46
  • thank you very much for your answer. I know what you mean now. I set the focusable true for the EditText. Now I can detect the focus change event: only when I touch the EditText. When I touch the outside of the EditText, the code can't detect focus changed event. So I set the focusable of the Dialog's layout. But this still doesn't work. I think out an poor way to deal with this: I have an imageview in the dialog. I set onclicklistener to this imageView to hide the keyboard. But this is not good. Do you have any idea? – Vigor Nov 05 '12 at 05:52
  • Vigor I did edit my answer. I did make the wrapper clicable and if someone clicks outside the edittext the keyboard will be set to hidden. – Greezer Nov 05 '12 at 07:49
1

Try by setting InputMethodManager.SHOW_FORCED for Edittext :

InputMethodManager input_manager = (InputMethodManager) 
                       getSystemService(Context.INPUT_METHOD_SERVICE);
input_manager.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213