Skip to content Skip to sidebar Skip to footer

How To Set Edittext Width To One Character

I'm developing an Android application and now I'm creating a password screen which will have four editText to hold four digits. This is my password.xml:

Solution 1:

Change it to android:ems="1"

<EditText
    android:id="@+id/firstNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="191dp"
    android:ems="1"
    android:inputType="textPassword"
    android:lines="1"
    android:maxLength="1"
    android:maxLines="1"
    android:minLines="1" >

http://developer.android.com/reference/android/R.attr.html#ems

An "em" is a term from typography - basically you can think of it as the width of 1 character at the specified font size. This is preferred as it will always be one character wide even if you change the font size.

http://en.wikipedia.org/wiki/Em_(typography)

Solution 2:

set android:maxEms="1" or remove wrap_content and set the width

android:layout_width="40dp"android:layout_height="40dp"

Solution 3:

If you want to set physical your edittext's physical width you can use

android:maxWidth="your size in dp"

but if you want to receive single character in it you can use

android:maxLength="1"

Post a Comment for "How To Set Edittext Width To One Character"