ab11 ab11 - 6 months ago 22
Android Question

In Android, how to create EditText of fixed width?

I would like to create an EditText which only excepts numbers, has a maximum of 4 numbers, and is never narrower than it would be if filled with 4 numbers (does not shrink when empty, or have to expand to accommodate 4 numbers, so it's fixed.)

The first 2 are accomplished with: android:inputType="number" and android:maxLength="4".

How can I set the minimum length?

Answer Source

A little hackish, but you can put your EditText inside a FrameLayout alongside with another TextView with text="0000" and visibility=invisible.

FrameLayout Should have width=wrap_content and your EditText will have width=match_parent.
That should make it the right width always.

The advantage of this is, it's 100% xml-side, no code involved.

<FrameLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0000"
    android:visibility="invisible" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:maxLength="4" />
</FrameLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download