Skip to content
Related Articles

Related Articles

How to Invoke Keyboard Programmatically in Android?

Improve Article
Save Article
  • Last Updated : 23 Feb, 2021
Improve Article
Save Article

Android System by defaults shows an on-screen keyboard when any UI element such as an Input Text element receives focus. For a better experience, a developer can explicitly specify the desired characteristics or any methods to invoke. Desired characteristics could be characters such as allowing only numerals, allowing only alphabets, or any other similar thing. Desired methods could be to provide an auto-correct function or provide an emoticon. An input text field might be present on the layout, and the developer does not wish it to gain focus unless invoked, or vice-versa. Because if it receives focus, the soft keyboard gets invoked, and there is a complete deviation from the actual context. Similarly, the reverse is persuaded in case the inputs are needed before showing any context. Through this article, we want to share with you how we can invoke the soft-keyboard, and this could be applied to any desired application. Note that we are going to implement this project using the Kotlin language. 


Keyboards are generally invoked by the Input Methods such as an EditText, yet we can invoke them without such input methods. There are two ways by which we can accomplish this task, by making considerable changes to:

  1. AndroidManifest.xml file (or)
  2. MainActivity.kt file


Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Go to either AndroidManifest.xml file or MainActivity.kt file

Go to either AndroidManifest.xml file or MainActivity.kt file and refer the following code. Note that both approaches can be implemented at a time.

Approach 1: Working with AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity"
        android:windowSoftInputMode="stateVisible"> <!--This entitity was
                                    explicitly added for desired result-->
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
<!--Refer here Again

Approach 2: Working with the MainActivity.kt file


import android.os.Bundle
import android.view.WindowManager
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Calls Soft Input Mode to make it Visible

Output observed in the Emulator from both the approaches:

Output observed in both the cases

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!