MainActivity.kt
package com.example.jetpack
import android.os.Bundle
import android.text.InputFilter
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 characters/alphabets/letters with white space
// programmatically accepts both uppercase and lowercase letters
editText2.filters = arrayOf(filter)
}
}
// input filter to allow only characters and whitespace
val filter = InputFilter { charSequence, i, i2, spanned, i3, i4 ->
if (charSequence == "") { // for backspace
return@InputFilter charSequence
}
if (charSequence.toString().matches("[a-zA-Z ]+".toRegex())) {
charSequence
} else ""
}
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 characters and whitespace 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:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:inputType="text|textVisiblePassword"
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:inputType="text|textVisiblePassword"
android:padding="12dp"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - EditText allow only numbers
- android kotlin - EditText allow only characters and numbers
- android kotlin - EditText numbers only programmatically
- android kotlin - EditText min length
- android kotlin - Change orientation without restarting activity
- android kotlin - Convert pixels to dp programmatically
- android kotlin - RecyclerView smooth scroll
- android kotlin - RecyclerView StaggeredGridLayoutManager example
- android kotlin - GridView OnItemClickListener
- android kotlin - RadioButton with image and text
- android kotlin - RadioButton onClick event
- android kotlin - TextView html formatted text
- android kotlin - Coroutines with timeout
- android kotlin - Room with coroutines
- android kotlin - Coroutines async