How to set minimum and maximum character limit for EditText in Android?

I want to set the minimum and maximum input value for the EditText window. I create one simple check for EditText; it takes values ​​AZ and 0-9 with a minimum value of 5 and a maximum of 8 characters.

I set the maximum and other checks as follows:

 <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 cannot set a minimum value. The EditText field is in the warning dialog box. 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();
    }`

EditText. EditText. setOnFocusChangeListener, . 5, , - EditText.

- .

+5
5
InputFilter[] lengthFilter = new InputFilter[1];
lengthFilter[0] = new InputFilter.LengthFilter(9); //Filter to 9 characters
mEditText.setFilters(lengthFilter);

.

0

XML

    <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_10dp">

            <EditText
            android:id="@+id/input_desc"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="@string/ticket_desc"
            android:inputType="textMultiLine"
            android:paddingStart="@dimen/dimen_5dp" /> 

           <TextView
            android:id="@+id/textView_desc_count"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end|center"
            android:paddingEnd="10dp"
            android:text="@string/desc_count" />

textViewDescCount = view.findViewById(R.id.textView_desc_count);
editTextTicketDesc = view.findViewById(R.id.input_desc);
textChangeOnType(editTextTicketDesc, 300, textViewDescCount);
0

, , , , , , 5 ,

if (myTextBox.length() < 5)
{
Log.d("APP", "Sorry, you need to enter at least 5 characters";
}
else
{
Log.d("APP", "Input valid");
}
-1
source

You can do something like this .... in this I checked the length of the character, and if it is more than 5, then editText loses focus. and after Touch on that, he returns focus ... I hope this helps 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;
        }
    });
-2
source

You can try under the code inside the listener:

If the condition is not met,

edittextKode.setFocus( true) ;
edittextKode.setFocusableInTouchMode( true)  ;
edittextalamat.setFocus( false) ;
edittextalamat.setFocusableInTouchuha( false) ;
edittextKode.requestFocus();
-3
source

All Articles