How to Define a Min and Max Value For Edittext in Android

Is there a way to define a min and max value for EditText in Android?

First make this class :

package com.test;

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

private int min, max;

public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}

public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}

private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}

Then use this from your Activity :

EditText et = (EditText) findViewById(R.id.myEditText);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")});

This will allow user to enter values from 1 to 12 only.

EDIT :

Set your edittext with android:inputType="number".

You can find more details at https://www.techcompose.com/how-to-set-minimum-and-maximum-value-in-edittext-in-android-app-development/.

Thanks.

How to set maximum value 1000000 of edittext in android?

you can use in java

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});

or in xml

  <EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="13"/>

how to define a min and max value for EditText in Android kotlin?

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main2.*

import kotlin.math.log

class MainActivity : AppCompatActivity() {


val total: Int = 40 //Max Limit
var first : Int = 0 //for first edit text GOALS
var second : Int = 0 // For Fails
var previous : Int = 0 //store Previous value for if user enter exceed limit
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

et1.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable) {
if(s.toString().equals("")){
first = 0
}
else {
first = et1.text.trim().toString().toInt()
}

val temp : Int = (second + first)
if(temp>40){
et1.setText(Integer.toString(previous))
first = previous
Log.v("ABCD", "MAX Limit is 40")
} else {

Log.v("ABCD", first.toString())
}

}

override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {
if(et1.text.trim().toString().equals("")){
previous = 0
} else {
previous = et1.text.trim().toString().toInt()
}
}

override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {

}
})
et2.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable) {

if(s.toString().equals("")){
second = 0
}
else {
second = et2.text.trim().toString().toInt()
}
val temp : Int = (second + first)
if(temp>40){
et2.setText(Integer.toString(previous))
second = previous
Log.v("ABCD", "MAX Limit is 40")
} else {
//et2.setText(Integer.toString(second))
Log.v("ABCD", second.toString())
}

}

override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {

if(et2.text.trim().toString().equals("")){
previous = 0
} else {
previous = et2.text.trim().toString().toInt()
}
}

override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {

}
})
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">


<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>

</LinearLayout>

Let's try. I hope you problem will be solved. Ask if you have any query. Thanks

android edittext minmum and maximum value

You should replace the line:

int input = Integer.parseInt(dest.toString() + source.toString());

With

int input = Integer.parseInt(source.toString());

Look at Can someone help me with the parameters to the Android InputFilter "filter" method? (plus regex) to understand why

Is there a way to define a min and max value for a EditText. EG 20 - 200 (not starting at 1)

there will be some event, Here I'm taking button click event

@Override
public void onClick(View view) {

String guessSize=editText.getText().toString();
if(Integer.parseInt(guessSize)<=20 || Integer.parseInt(guessSize)>=200)
{
//Anything you want
Toast.makeText(MainActivity.this, "No wroking", Toast.LENGTH_SHORT).show();
}

How to fix minimum and maximum value in double value for Edit Text in android?

package com.test;

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

private double min, max;

public InputFilterMinMax(double min, double max) {
this.min = min;
this.max = max;
}

public InputFilterMinMax(String min, String max) {
this.min = Double.parseInt(min);
this.max = Double.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, double start, double end, Spanned dest, double dstart, double dend) {
try {
double input = Double.parseDouble(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}

private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}

How to limit the text in numbers only from 0-59 in Edit Text in Android?

Use this code in XML to allow numbers only in Edit Text:

<EditText android:id="@+id/edit_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numbers" />

To control the text values entered in that EditText:

edittext.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}

public void beforeTextChanged(CharSequence s, int start, int count, int after){}

public void onTextChanged(CharSequence s, int start, int before, int count){
String strEnteredVal = edittext.getText().toString();

if(!strEnteredVal.equals("")){
int num=Integer.parseInt(strEnteredVal);
if(num<60){
edittext.setText(""+num);
}else{
edittext.setText("");
}
}

});


Related Topics



Leave a reply



Submit