0

I want to hide keyboard after clicking on EditText in android i tried below code but its not working.

 mPassword.setInputType(InputType.TYPE_CLASS_NUMBER);
    mPassword.requestFocus();
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mPassword, InputMethodManager.RESULT_HIDDEN);

I have also given android:windowSoftInputMode="stateHidden" in activity manifest. still i'm getting keyboard.Please tell me how can i hide soft keyboard??

3
  • why would you want to hide keyboard on edittext focus?? Commented Oct 23, 2013 at 6:55
  • i want to use my own keyboard Commented Oct 23, 2013 at 6:56
  • Did you try my answer? Let me know if it helped you, please. Commented Oct 24, 2013 at 18:45

4 Answers 4

1

You can use the following code to hide the soft keyboard

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mPassword.getWindowToken(), 0);

Also,

If you want to hide when the activity starts, then edit your manifest file as

<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden"  />
Sign up to request clarification or add additional context in comments.

1 Comment

its not waorking.still after clicking on i'm getting keyboard. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mPassword.getApplicationWindowToken(), 0);
0

Use following code in your manifest file.

<activity
     android:name="YourActivity"
     android:configChanges="keyboardHidden"
     android:windowSoftInputMode="stateHidden"/>

Comments

0

Try like this,

your_edittext.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (m != null) {
                    m.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
                    your_edittext.clearFocus();
                }
            }
        });

Comments

0

Here's the solution which will hide the keyboard from anywhere.

1st create in your activity of choice the listener for the state and the method that will do the closing (based on the open state).

public class MainActivity extends SherlockFragmentActivity {

  private boolean mKeyboardOpen = false;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate()");

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // add "keyboard open listener"
    final View v = findViewById(R.id.pager);
    v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        int hRoot = v.getRootView().getHeight();
        int hView = v.getHeight();
        int heightDiff = hRoot - hView;
        // if more than 150 pixels, its probably a keyboard...
        mKeyboardOpen = heightDiff > 150;
        Log.d(TAG, "hRoot=" + hRoot + ", hView=" + hView + ", mKeyboardOpen=" + mKeyboardOpen);
      }
    });
  }


  public void closeSoftKeyboard() {
    if (mKeyboardOpen) {
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
  }
}

2nd call ((MainActivity) getActivity()).closeSoftKeyboard(); from anywhere, e.g. your EditText's OnClickListener().

Hint: I'm using the ViewPager root view (R.id.pager), but you should probably replace it with your view root id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.