3

We are trying to hide the soft keyboard during development. Translated we do not ever want to see it. Here is the configuration Nexus 9 API 28 Project SDK 26 Project is Kotlin Here is the code for the Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidstackoverflow.devconstraint"
android:windowSoftInputMode="stateAlwaysHidden">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"

    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:windowSoftInputMode="stateAlwaysHidden">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LayOutActivity"
        android:windowSoftInputMode="stateAlwaysHidden">
    </activity>
</application>

We have tried every line of code in this SO Question Question

code in LayOutActivity

open class LayOutActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_lay_out)
    this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    val view = currentFocus
    //if (view != null) {
        //val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //imm.hideSoftInputFromWindow(view.windowToken, 0)
    //}
    //val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    //if (imm.isActive)
    //imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)

    //hideSoftKeyboard(view = null)


    //UIHelper.hideSoftKeyboard(activity = Activity())
        doALL()


}

//fun hideSoftKeyboard(view: View?) {
    //val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    //inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)
//}
fun doALL(){
    //UIHelper.hideSoftKeyboard(activity = Activity())
    UIHelper.hideSoftKeyboard(view = null)
    UIHelper.hideKeyboard(this,etOne)
    etOne.setText("I have new Text")
}


object UIHelper {

    fun hideSoftKeyboard(activity: Activity?) {
        if (activity != null) {
            val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (activity.currentFocus != null && inputManager != null) {
                inputManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
                inputManager.hideSoftInputFromInputMethod(activity.currentFocus!!.windowToken, 0)
            }
        }
    }

    fun hideSoftKeyboard(view: View?) {
        if (view != null) {
            val inputManager = view!!.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager?.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
        }
    }

    fun hideKeyboard(activityContext: Context, editText: EditText) {
        editText.requestFocus()
        Handler().postDelayed({
            val inputMethodManager = activityContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.showSoftInput(editText, InputMethodManager.HIDE_IMPLICIT_ONLY)
        }, 250)
    }
}

}

We just want to know how to keep the soft keyboard hidden We have used the one line of code in the LayOutActivity before and it worked Is this a new issue with Android 8 or with Kotlin ? here is our one line that worked

this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
Vector
  • 2,443
  • 5
  • 19
  • 35

1 Answers1

1

This should work:

val inputManager: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.SHOW_FORCED) // It can be done by show_forced too

Also, in AndroidManifest.xml:

android:windowSoftInputMode="stateHidden"

Also, if you have EditText in there, try using:

editText.setShowSoftInputOnFocus(false);

Check this out: https://stackoverflow.com/a/49534949/4409113

And also: Android - Hide keyboard on Android 8

ʍѳђઽ૯ท
  • 15,369
  • 7
  • 47
  • 103
  • We posted your code to the Manifest and in the Activity on Create with this error currentFocus must not be null so how do we make currentFocus not null – Vector Sep 23 '18 at 20:00
  • Updated the answer. You can however put an API check then setting the configuration: https://stackoverflow.com/a/49759277/4409113 – ʍѳђઽ૯ท Sep 23 '18 at 20:06
  • I did read the Android-Hide keyboard on Android 8 just added your editText code We have Min 19 but fixed that with @RequiresApi(Build.VERSION_CODES.LOLLIPOP) will keep reading BIG concern is where is this code placed in the onCreate I assume? – Vector Sep 23 '18 at 20:12
  • Well yes. However it depends on you. For example, if you place those codes inside `onCreate()` when `Activity` creates, it must not show the keyboard when showing the `Activity`. – ʍѳђઽ૯ท Sep 23 '18 at 20:18
  • OK because I am new at Kotlin it took a while to figure out he currentFocus not null ERROR here is what needs to change in your code currentFocus?.windowToken NOTICE the wonderful "?" – Vector Sep 23 '18 at 20:19
  • as soon as I type in the EditText up pops the keyboard so any Thoughts – Vector Sep 23 '18 at 20:24
  • Android studio always give the best solution-option, well yeah, "?" operator can help (And I've changed the codes because of that) Anyways, updated the answer. What do you mean `up pops the keyboard`? It hides the keyboard or something? – ʍѳђઽ૯ท Sep 23 '18 at 20:26
  • YES as soon as I type in the EditText up pops the KeyBoard IT does NOT happen when we give focus to the EditText trying some of the code in the Android 8 post not much luck so far – Vector Sep 23 '18 at 20:33
  • How about deleting this line of code then checking?: `setShowSoftInputOnFocus(false);` – ʍѳђઽ૯ท Sep 23 '18 at 20:35
  • Yes I did delete that no Luck – Vector Sep 23 '18 at 20:41
  • So try removing this and related codes in `AndroidManifest.xml`: `android:windowSoftInputMode="stateHidden"` and then it should work fine. – ʍѳђઽ૯ท Sep 23 '18 at 20:42
  • NO need to have setShowSoftInputOnFocus and REALLY need the two lines of code in the Manifest THE ISSUE is with the entering text in the EditText – Vector Sep 23 '18 at 20:48
  • There might be some issues in xml code of `EditText` maybe? Could you elaborate more by adding a picture, gif or etc? Also, I think that should be pasted in another new question with full xml code in order to be able to help you. – ʍѳђઽ૯ท Sep 23 '18 at 20:52
  • 1
    After a lot of painful testing it seems the issue is with the Nexus 9 with API 28 your code and a lot of other code just does not work on this Emulator I tested on a Nexus 6 with API 28 and your code works great Thanks for taking the time. Any one else with this problem you need the code in the Manifest and the code in any Activity you do not want the softinput keyboard to show up as for Emulator testing either put API 26 on the Nexus 9 or use a another Tablet Emulator – Vector Sep 24 '18 at 01:04