Home » Android » android – Hide Soft Input in OnCreate

android – Hide Soft Input in OnCreate

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a TableLayout that I am dynamically adding content to in code in OnCreate. Once the Activity creates, it is focusing on one of my dynamically created EditTexts and displaying the keyboard. I don’t want the keyboard to display until the user specifically presses one of the EditTexts. I’ve tried:

InputMethodManager input = (InputMethodManager) GetSystemService(InputMethodService);
input.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);

But the keyboard still displays and CurrentFocus returns null. So when I attempt to specifically point the focus to another view and then perform the above like:

InputMethodManager input = (InputMethodManager) GetSystemService(InputMethodService);
title.FindFocus();
input.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);

CurrentFocus is still null and the keyboard still displays. title is a TextView that I already have an instance of in code. Can I just not give focus to a TextView or is there something else I’m missing?

How to&Answers:
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Answer:

Very Easy

  1. Go to your AndroidManifest.xml file
  2. In your activity add: android:windowSoftInputMode="stateHidden"

For example:

<activity
         android:name=".package.example.MyActivity"
            android:windowSoftInputMode="stateHidden"/>

Answer:

  1. Go to AndroiManifest.xml file
  2. Activity in which you want to hide keyboard add the following thing

    android:windowSoftInputMode=”stateHidden|adjustResize|stateAlwaysHidden”