How to Set Minimum and Maximum Input Value in EditText in Android?

In Android, Editext is a subclass of textview. Editext is used for giving textual input such as strings, characters, strings, numbers, etc. Editext is generally used for getting text data. In Edittext not limited to entered characters or others. In This article, we will show you how you could set a minimum and a maximum input value in the edit text of  mobile application.


Here is the step by step Implementation process:


Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside activity_main.xml file.

 <?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/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="28dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_centerHorizontal="true"
android:layout_marginStart="20dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="20dp"
android:gravity="center"
android:hint="Enter number between 1-100"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Create an MainActivity.kt file in your package folder .

 implement the same invoke the following code inside MainActivity.kt file.

 import android.os.Bundle
import android.text.InputFilter
import android.text.Spanned
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivityone : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_activityone)

val editText = findViewById<EditText>(R.id.editText)

editText.filters = arrayOf<InputFilter>(MinMaxFilter(1, 100))
}

inner class MinMaxFilter() : InputFilter {
private var intMin: Int = 0
private var intMax: Int = 0

//data pass of constructor
constructor(minValue: Int, maxValue: Int) : this() {
this.intMin = minValue
this.intMax = maxValue
}

//filter method
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dStart: Int, dEnd: Int): CharSequence? {
try {
val input = Integer.parseInt(dest.toString() + source.toString())
if (isInRange(intMin, intMax, input)) {
return null
}
} catch (e: NumberFormatException) {
e.printStackTrace()
}
return ""
}

private fun isInRange(a: Int, b: Int, c: Int): Boolean {
return if (b > a) c in a..b else c in b..a
}
}
}
Step 4: Output of above example

 
Happy coding!

365Bloggy May 24, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive