MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
class MainActivity : Activity() {
private lateinit var editText:EditText
private lateinit var textView:TextView
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)
textView = findViewById(R.id.textView)
// set edit text max length to 10 programmatically
val maxLength = 10
editText.setMaxLength(maxLength)
// add text changed listener to edit text
editText.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(p0: CharSequence?,
p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?,
p1: Int, p2: Int, p3: Int) {
// count number of inputted characters in edit text
textView.text = "${p0?.toString()?.length}/$maxLength"
}
override fun afterTextChanged(p0: Editable?) {
}
})
}
}
// extension function to set edit text maximum length
fun EditText.setMaxLength(maxLength: Int){
filters = arrayOf<InputFilter>(
InputFilter.LengthFilter(maxLength))
}
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="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="8dp"
android:fontFamily="sans-serif-condensed-medium"
android:inputType="text|textVisiblePassword"
android:padding="12dp"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:fontFamily="sans-serif-condensed-medium"
android:textStyle="bold"
android:gravity="center"
android:padding="8dp"
android:textColor="#4F42B5"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/editText"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - NumberPicker text size
- android kotlin - NumberPicker text color
- android kotlin - Disable soft keyboard on NumberPicker
- android kotlin - EditText space validation
- android kotlin - EditText email validation
- android kotlin - EditText select all on focus
- android kotlin - EditText first letter capitalization
- android kotlin - EditText allow only certain characters
- android kotlin - Set EditText digits programmatically
- android kotlin - EditText allow only positive numbers
- android kotlin - EditText allow only characters and numbers
- android kotlin - EditText numbers only programmatically
- android kotlin - EditText min length
- android kotlin - EditText rounded corners programmatically
- android kotlin - EditText border color programmatically