'How to set minimum and maximum characters limitation to EditText in Android?
I want to set minimum and maximum input value for EditText box. I am creating one simple validation for EditText; it takes A-Z and 0-9 values with minimum 5 and maximum 8 character.
I set the maximum and other validations as follow:
<EditText
android:id="@+id/edittextKode_Listing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_alignTop="@+id/textKode_listing"
android:layout_toRightOf="@+id/textKode_listing"
android:maxLength="8"
android:inputType="textCapCharacters"
android:digits="0,1,2,3,4,5,6,7,8,9,ABCDEFGHIJKLMNOPQRSTVUWXYZ"
/>
but not able to set minimum value requirement. My EditText box is in an alert dialog. I tried the following code to solve the problem:
private void openInboxDialog() {
LayoutInflater inflater = this.getLayoutInflater();
// declare dialog view
final View dialogView = inflater.inflate(R.layout.kirim_layout, null);
final EditText edittextKode = (EditText) dialogView.findViewById(R.id.edittextKode_Listing);
final EditText edittextalamat = (EditText) dialogView.findViewById(R.id.edittextAlamat);
edittextKode.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(edittextKode.getText().toString().length() > 0){
if(edittextKode.getText().toString().length() < 5)
{
edittextKode.setError("Error");
Toast.makeText(GPSActivity.this, "Kode listing value not be less than 5", Toast.LENGTH_SHORT).show();
edittextKode.requestFocus();
}
}
}
});
final AlertDialog.Builder builder = new AlertDialog.Builder(GPSActivity.this);
builder.setTitle("Kirim").setView(dialogView)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
gpsCoordinates = (TextView) findViewById(R.id.text_GPS_Coordinates);
kode = edittextKode.getText().toString();
alamat = edittextalamat.getText().toString();
catatan = edittextcatatan.getText().toString();
pengirim = edittextPengirim.getText().toString();
if (kode.length() > 0 && alamat.length() > 0
&& catatan.length() > 0 && pengirim.length() > 0) {
message = "Kode listing : " + kode + "\nAlamat : "
+ alamat + " \nCatatan : " + catatan + " \n Pengirim : " + pengirim
+ "\nKoordinat GPS : "
+ gpsCoordinates.getText().toString();
sendByGmail();
} else {
Toast.makeText(
getApplicationContext(),
"Please fill all three fields to send mail",
Toast.LENGTH_LONG).show();
}
}
});
builder.create();
builder.show();
}`
In this alert dialog, I have two EditText boxes. I want to apply my validation on first EditText. I called the setOnFocusChangeListener to check its minimum length on focus change. If the length is less than 5 then requested for focus, but it still types in second EditText box.
Could anyone please help me.
Solution 1:[1]
InputFilter[] lengthFilter = new InputFilter[1];
lengthFilter[0] = new InputFilter.LengthFilter(9); //Filter to 9 characters
mEditText.setFilters(lengthFilter);
This creates a filter to limit the number of characters.
Solution 2:[2]
There is no direct way to do this, the only way of doing it, as far as I know, is when the edit text loses focus or when the user submits the information you do length check and see if its less than 5 characters, e.g.
if (myTextBox.length() < 5)
{
Log.d("APP", "Sorry, you need to enter at least 5 characters";
}
else
{
Log.d("APP", "Input valid");
}
Solution 3:[3]
You can do something like this way....in this i checked the character length and if it is greater than 5 then the editText loses the focus..and After Touch on that it gets back the Focus..Try this ..i hope this will help you..
edittextKode.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
if(arg0.length()>5)
{
Toast.makeText(getApplicationContect(),"Max.value is 5",1).show();
edittextKode.setFocusable(false);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
edittextKode.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
// TODO Auto-generated method stub
edittextKode.setFocusableInTouchMode(true);
return false;
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dinesh |
| Solution 2 | Boardy |
| Solution 3 |
