Mehdiway
Mehdiway

Reputation: 10636

Android - How to set numeric EditText to a specified number of digits

I want an EditText to contain exactly 8 digits (like bank accounts). Of course in addition to :

android:inputType="number"

Is there an option in XML or do I have to manage this programmatically ?

Upvotes: 0

Views: 2703

Answers (5)

ASP
ASP

Reputation: 447

Try the following line in your EditText:

android:maxLength="8"

Upvotes: 0

Veaceslav Gaidarji
Veaceslav Gaidarji

Reputation: 4301

Programmatically.

Also, you can save time by using existing library EditText with validation. You can use a regex type with following pattern [0-9]{8}.

Upvotes: 1

This is my phone edit text:

<EditText
    style="@style/ObligatoryEditTextStyle"
    android:id="@+id/etxtClientPhone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:gravity="left"
    android:textColor="@color/hub_blue_normal"
    android:visibility="visible"
    android:selectAllOnFocus="true"
    android:digits="0123456789"
    android:maxLength="8"/>

Tell me if I helped you and good programming!!

Upvotes: 0

Michael
Michael

Reputation: 324

Change input type in decimal, then set maxLength="8".

android:inputType="decimal"
android:maxLength="8"

Upvotes: -1

BHuelse
BHuelse

Reputation: 2981

I think there is no way just in xml. You have to validate it programmatically.

@Override
public void afterTextChanged(Editable s) {
    // validation code goes here
}

Upvotes: 1

Related Questions