How to Invoke Keyboard Programmatically in Android?
Last Updated :
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.
Implementation
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
Approach
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 Again
android:windowSoftInputMode="stateVisible"
-->
Approach 2: Working with the MainActivity.kt file
Kotlin
import android.os.Bundle
import android.view.WindowManager
import 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)
}
}
Output observed in the Emulator from both the approaches:
Similar Reads
How to programmatically hide Android soft keyboard In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app i
2 min read
How to Get RAM Memory in Android Programmatically? RAM (Random Access Memory) of a device is a system that is used to store data or information for immediate use by any application that runs on the device. Every electronic device that runs a program as a part of its application has some amount of RAM associated with it. Mobile devices nowadays come
3 min read
How to Check GPS is On or Off in Android Programmatically? GPS (Global Positioning System) is a satellite-based navigation system that accommodates radio signals between satellite and device to process the device's location in the form of coordinates. GPS gives latitude and longitude values of the device. Recent mobile phones are equipped with GPS modules t
2 min read
How to Check if AUX is Plugged or Not in Android Programmatically? AUX (Auxiliary) port is a common port observed in most of the daily use electronic devices. It is a standard communication port for transferring audio signals from one device to another. We mostly observe them on mobile phones, computers, television sets, speakers, headphones, and headsets. While pl
2 min read
How to Fetch Device ID in Android Programmatically? The android Device ID is a unique code, string combinations of alphabets and numbers, given to every manufactured android device. This code is used to identify and track each android device present in the world. In Android, the Device ID is typically related to the Google Play Services and is most c
3 min read
How to Detect Tablet or Phone in Android Programmatically? A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a
3 min read