How to set Minimum and Maximum Character limitation to EditText in Android?
Posted
by
nishitpatel
on Stack Overflow
See other posts from Stack Overflow
or by nishitpatel
Published on 2013-01-22T08:10:18Z
Indexed on
2014/08/24
4:21 UTC
Read the original article
Hit count: 178
android
|android-layout
I am new to android here i have very silly problem i want to set my Edit text box minimum and maximum input value. Here I am creating one Simple validation for Edit text it only take A-Z and 0-9 value with minimum 5 and Maximum 8 character.
I set the Maximum and other validation 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 requirement. My Edit text is in alert dialog and i also apply the following code to solve this 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 i want to apply my validation on first edittext i called the setOnFocusChangeListener to check its minimum length on focus change and if length is less than 5 request for focus but it still type in second edittext.
please help me out.
© Stack Overflow or respective owner