Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I would like to create an EditText which accepts only 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?
–
–
–
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>
–
–
–
In my situation, I needed the view to be 6 digits wide. android:ems made the field too wide (since the M-character is wider than a digit).
I ended up using a transparent hint which dictates the minimum width of the field. The example below is six digits wide.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="000000"
android:textColorHint="#00FFFFFF"
android:maxLength="6"
android:inputType="numberPassword" />
This solution is independent of font, letter spacing and other things which may vary. The textColorHint is a 0% opaque white color, so the hint should never be visible to the user.
. . . this should insure the tightest fit possibly allowed by the default android edittext style
For additional control with the padding on both left and right you can use
android:background="#ffffff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
There is an incredibly easy way to do this programmatically.
myEditText.setLayoutParams(new LinearLayout.LayoutParams(200, 50));
This would set the width to 200 and the height to 50!!
Enjoy :)
–
The problem is, that an EditText widget can loss focus because of a lot of different actions like touching another sensitive view, menu button or (hard keyboard cursor control) and so on. The restiction of a maximum length or type of the input can be done during the tying process (accept the character if it passes the filter or the length is not exceeded). but focus lost can be occour every time - even the submission is not completed yet and the input to short -> therefore there is no build-in minimum length solution, because the application would block at this point the execution of another event or task.
At least you have to validate your input before going to the next step in your workflow.
I use android:layout_width="60dip" so agree with mike fratto.
The ems solution appears to be interesting as well.
You can set alignment other wise when you put character more than wditText width than It's increase width or EditText width.
Please set toLeftOf and toRightOf of the above view to manage this things. Only one solution
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/resetpage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:layout_marginTop="80dp"
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:textSize="20sp"
android:gravity="top"
android:background="@drawable/border"
</LinearLayout>
write this code inside the linear layout or u can customize the constraints
of margin in the editText as per your layout and the background is
just for the border of the editText in the drawable folder.
**border.xml**-->just paste this file in the drawable folder
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<solid android:color="#D0D0D0"/>
<corners android:radius="10dp" />
</shape>
</item>
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/browser_actions_bg_grey"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.