Android kotlin: Showing Soft Keyboard on EditText Focus

Android Kotlin - Showing Soft Keyboard on EditText Focus

This code demonstrates how to programmatically show the soft keyboard when an EditText receives focus in an Android application written in Kotlin.

The code consists of two parts: the MainActivity class responsible for handling user interaction and the showSoftKeyboard extension function that provides a concise way to show the keyboard on any EditText instance.

Code Breakdown

MainActivity.kt:

  1. Variable Initialization: The class declares two lateinit variables, editText and button, to hold references to the EditText and Button widgets from the layout.
  2. onCreate: This method sets up the activity when it's first created.
    • It inflates the activity_main.xml layout using setContentView.
    • It retrieves references to the EditText and Button using findViewById.
    • An OnClickListener is set on the Button. When clicked, it:
      • Requests focus on the EditText using requestFocus.
      • Calls the showSoftKeyboard extension function on the EditText to display the keyboard.

showSoftKeyboard Extension Function:

  • This function takes an EditText instance as a receiver.
  • It retrieves the InputMethodManager service from the context and casts it to the appropriate type.
  • It calls the showSoftInput method on the InputMethodManager instance, passing the EditText and a flag (InputMethodManager.SHOW_IMPLICIT) to display the keyboard.

activity_main.xml:

  • This file defines the layout for the activity using ConstraintLayout.
  • It contains two elements:
    • An EditText with the ID editText. This is where the user types input.
    • A Button with the ID button. Clicking this button requests focus on the EditText.

Summary

This code snippet provides a clean and efficient way to show the soft keyboard when an EditText is focused in an Android application. By separating the logic into an extension function, the code becomes more reusable and easier to maintain.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context.INPUT_METHOD_SERVICE
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText


class MainActivity : Activity() {
    private lateinit var editText:EditText
    private lateinit var button:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        editText = findViewById(R.id.editText)
        button = findViewById(R.id.button)

        // focus edit text on button click
        button.setOnClickListener {
            // request focus on edit text
            editText.requestFocus()

            // when edit text focused show soft keyboard
            editText.showSoftKeyboard()
        }
    }
}


// extension function to open soft keyboard programmatically
fun EditText.showSoftKeyboard(){
    (this.context.getSystemService(
        INPUT_METHOD_SERVICE) as InputMethodManager)
        .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="32dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:padding="12dp"
        android:textSize="30sp"
        android:inputType="text|textVisiblePassword"
        android:fontFamily="sans-serif-condensed-medium"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Focus EditText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - Show soft keyboard when EditText is focused
More android kotlin tutorials