keyboard show hide in android programmatically code example

Example: how to open soft keyboard in android programmatically

class MainActivity: AppCompatActivity() {

	override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message)
		val editTextMessage = findViewById<EditText>(R.id.myEditeText)
      	showSoftKeyboard(this, editTextMessage)
    }


  	private fun showSoftKeyboard(context: Context, v: View) {
    	val iim = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    	if (v.requestFocus()) {
      		iim.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT)
    	}
  	}
}

Tags:

Java Example