title: How to set minimum and maximum characters limitation to EditText in Android?
body: <p>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.</p>
  
  <p>I set the maximum and other validations as follow: </p>
  
  <pre><code> &lt;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"
          /&gt;
  </code></pre>
  
  <p>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:  </p>
  
  <pre><code>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() &gt; 0){
                      if(edittextKode.getText().toString().length() &lt; 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() &gt; 0 &amp;&amp; alamat.length() &gt; 0
                                  &amp;&amp; catatan.length() &gt; 0 &amp;&amp; pengirim.length() &gt; 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();
      }`
  </code></pre>
  
  <p>In this alert dialog, I have two EditText boxes. I want to apply my validation on first EditText. I called the <strong>setOnFocusChangeListener</strong> 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.</p>
  
  <p>Could anyone please help me.</p>