How to Invoke Keyboard Programmatically in Android?
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
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
Approach 2: Working with the MainActivity.kt file