Wednesday, February 1, 2017

How to set width of EditText in android

I wanted to create an EditText box of specific width in Android (160 characters - the standard SMS size)

The best approach I found is to create a hint message of required characters
Set hint message as EditText hint with 0% opacity as hint color. So that it is invisible. :)

Example : To create an edit text of 160 characters I created a hint message of 160 characters

Layout.xml
<EditText    android:layout_width="wrap_content"    android:layout_height="wrap_content"   
 android:gravity="top"    android:id="@+id/input"    android:hint="@string/hint_message"   
 android:textColorHint="#00FFFFFF"/>


Strings.xml
<string name="hint_message">Enter message here.............................................................................................................................................</string>
Note: Setting maxLength attribute of EditText will only decide the number of characters the user can input. It does not have any effect on physical width of EditText
Do comment if this helped you. Happy coding! :) 

No comments:

Post a Comment