0

How can I ensure that the text entered on this edittext is nothing but 13digits. This is my attempt but the user can enter 5 digits but I want it 13 digits

<EditText
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:maxLength="13"
        android:layout_below="@+id/TextView01"
        android:layout_centerHorizontal="true"
        android:ems="10"/>

updated codes

final EditText phone = (EditText)findViewById(R.id.phone);

            InputFilter[] filterArray = new InputFilter[1];
            filterArray[0] = new InputFilter.LengthFilter(11);
            phone.setFilters(filterArray);

if(Double.parseDouble(phone.getText().toString()) > 11)
            {
                //the above if text always fails
            }
Blaze
  • 2,269
  • 11
  • 40
  • 82

1 Answers1

2

I don't know exactly what you want to do, but maybe you could try one of the following:

  1. don't setText unless edittext.getText().length()==13 (as I asume you will set it at a certain moment). If it's not the length you want, you can show a message, let's say
  2. add a TextWatcher (through onTextChangedListener) on edittext and keep track of user input
Rucsi
  • 284
  • 1
  • 10