MainActivity.kt
package com.example.jetpack
import android.os.Bundle
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// allow only positive numbers and decimal numbers programmatically
editText2.positiveNumbersOnly()
}
}
// extension function to allow only positive numbers and decimal numbers
fun EditText.positiveNumbersOnly(){
// numbers only soft keyboard
inputType = InputType.TYPE_CLASS_NUMBER
// specify the only accepted digits with point
keyListener = DigitsKeyListener.getInstance("0123456789.")
}
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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FEFEFA"
tools:context=".MainActivity">
<!-- allow only positive numbers and decimal numbers in xml -->
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:padding="12dp"
android:textSize="30sp"
android:inputType="number"
android:digits="0123456789."
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" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="12dp"
android:padding="12dp"
android:textSize="30sp"
android:fontFamily="sans-serif-condensed-medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - Volley image request
- android kotlin - Volley string request
- android kotlin - Chip center text
- android kotlin - ChipGroup get selected chips
- android kotlin - ChipGroup add chip programmatically
- android kotlin - NumberPicker remove divider
- android kotlin - NumberPicker divider color
- android kotlin - EditText password validation
- android kotlin - EditText phone number validation
- android kotlin - EditText live characters count
- android kotlin - EditText first letter capitalization
- android kotlin - Show soft keyboard when EditText is focused
- android kotlin - EditText hide error
- android kotlin - EditText allow only certain characters
- android kotlin - Set EditText digits programmatically