MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.res.Resources
import android.os.Bundle
import android.widget.NumberPicker
class MainActivity : Activity() {
private lateinit var numberPicker: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)
// 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
numberPicker.minValue = 0
numberPicker.maxValue = colors.size - 1
// set the valued to be displayed in number picker
numberPicker.displayedValues = colors
// set number picker divider height in pixels
// for api level 29 and up
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.Q) {
// set divider height in pixels
numberPicker.selectionDividerHeight = 20
}else{
numberPicker.setDividerHeight(20)
}
}
}
// set number picker divider height in pixels for api level up to 28
fun NumberPicker.setDividerHeight(height: Int) {
val pickerFields = NumberPicker::class.java.declaredFields
for (pf in pickerFields) {
if (pf.name == "mSelectionDividerHeight") {
pf.isAccessible = true
try {
// set divider height in pixels
pf.set(this, height)
} catch (e: java.lang.IllegalArgumentException) {
// log exception here
} catch (e: Resources.NotFoundException) {
// log exception here
} catch (e: IllegalAccessException) {
// log exception here
}
break
}
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="32dp">
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Volley JsonArrayRequest
- android kotlin - Volley JsonObjectRequest
- android kotlin - Volley image request
- android kotlin - Chip border color
- android kotlin - Chip background color
- android kotlin - ChipGroup single selection
- android kotlin - NumberPicker string values
- android kotlin - NumberPicker remove divider
- android kotlin - NumberPicker divider color
- android kotlin - Disable soft keyboard on NumberPicker
- android kotlin - EditText password validation
- android kotlin - EditText phone number validation
- android kotlin - EditText select all on focus
- android kotlin - EditText live characters count
- android kotlin - EditText first letter capitalization