Disabling Soft Keyboard on NumberPicker in Android with Kotlin
This code demonstrates how to programmatically prevent the soft keyboard from appearing when a user interacts with a specific NumberPicker
in an Android application written in Kotlin.
The code defines a MainActivity
class that extends the Activity
class. Inside onCreate()
, it inflates the layout file activity_main.xml
and retrieves references to two NumberPicker
widgets using findViewById
.
NumberPicker 1 - Setting Numeric Values:
The first NumberPicker
is configured with numeric values. It sets the minimum value to 0 and the maximum value to 20.
NumberPicker 2 - Setting String Values and Disabling Soft Keyboard:
The second NumberPicker
showcases a more complex setup. An array named colors
is created containing string values representing colors. The NumberPicker
's minimum and maximum values are set based on the array size. Finally, the displayedValues
property is assigned the colors
array to display these string values on the picker.
Crucially, the line numberPicker2.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
disables the soft keyboard for this specific NumberPicker
. This leverages the descendantFocusability
property of NumberPicker
and sets it to a constant value FOCUS_BLOCK_DESCENDANTS
. This instructs the system to prevent any of the NumberPicker
's descendants (including the edit text used for manual input) from receiving focus, effectively stopping the soft keyboard from appearing when the user interacts with this NumberPicker
.
The provided activity_main.xml
file defines the layout for the activity. It includes two NumberPicker
widgets with appropriate constraints within a ConstraintLayout
. Interestingly, it also includes a commented-out line setting android:descendantFocusability="blocksDescendants"
on the first NumberPicker
in the XML itself. This approach achieves the same outcome as the code in onCreate
but demonstrates an alternative way to disable the soft keyboard through layout attributes.
Summary
This code snippet provides two methods for disabling the soft keyboard on a NumberPicker
in Android with Kotlin. The first approach uses code within the activity's onCreate
method, while the second leverages an XML attribute. Both methods achieve the same goal of preventing the soft keyboard from appearing when the user interacts with the specified NumberPicker
.
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.widget.NumberPicker
class MainActivity : Activity() {
private lateinit var numberPicker:NumberPicker
private lateinit var numberPicker2:NumberPicker
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
numberPicker = findViewById(R.id.numberPicker)
numberPicker2 = findViewById(R.id.numberPicker2)
// set first number picker minimum and maximum value
numberPicker.minValue = 0
numberPicker.maxValue = 20
// display string values to the number picker
// at first, initialize a new array instance with string values
val colors = arrayOf("Red", "Green", "Blue", "Yellow",
"Magenta", "Black", "Pink")
// set the number picker minimum and maximum values
numberPicker2.minValue = 0
numberPicker2.maxValue = colors.size - 1
// set the valued to be displayed in number picker
numberPicker2.displayedValues = colors
// disable soft keyboard programmatically
numberPicker2.descendantFocusability =
NumberPicker.FOCUS_BLOCK_DESCENDANTS
}
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="32dp">
<!--
disable soft keyboard in xml
android:descendantFocusability="blocksDescendants"
-->
<NumberPicker
android:id="@+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:descendantFocusability="blocksDescendants"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/numberPicker" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - NumberPicker text size
- android kotlin - NumberPicker text color
- android kotlin - EditText password validation
- android kotlin - EditText phone number validation
- android kotlin - EditText enter key listener
- android kotlin - EditText listener
- android kotlin - EditText TextWatcher
- android kotlin - ImageView rounded corners programmatically
- android kotlin - ImageView set image from drawable
- android kotlin - ImageView set image from url
- android kotlin - Enable disable bluetooth programmatically
- android kotlin - Change screen orientation programmatically
- android kotlin - Change orientation without restarting activity
- android kotlin - RecyclerView GridLayoutManager example
- android kotlin - RecyclerView StaggeredGridLayoutManager example