How to Invoke Keyboard Programmatically in Android? - GeeksforGeeks

23 Feb, 2021
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:
AndroidManifest.xml file (or)MainActivity.kt file
AndroidManifest.xml file (or)
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
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.ao1">      <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="stateVisible"> <!--This entitity was                                    explicitly added for desired result-->            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  </manifest>  <!--Refer here Againandroid:windowSoftInputMode="stateVisible"-->
Approach 2: Working with the MainActivity.kt file
Kotlin
import android.os.Bundleimport android.view.WindowManagerimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Calls Soft Input Mode to make it Visible        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)    }}
Android-Misc
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
Retrofit with Kotlin Coroutine in Android
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters
Kotlin when expression