Reputation: 4360
Here is my EditText
layout:
<EditText
android:id="@+id/edittext_information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColorHint="@color/grey"
android:maxLength="4096"
android:hint="@string/default_information"
android:inputType="textMultiLine"
android:minLines="1"
android:lines="1"
android:maxLines="11"
android:scrollbars="vertical"
android:layout_marginTop="0dp"
android:background="@android:color/transparent"
android:padding="10dip"
android:gravity="top"/>
`
The problem is that my default_information
string is very long and occupies four lines. So initially EditText
is of four lines. But when user tries to enter some text, EditText
doesn't automatically gets resized up to entered text (for e.g. one line) It remains four lines only.
How to instruct EditText
to get resized to entered text when hint text is long?
Update:
Please see here is GIF demonstrating what problem I am facing:
Upvotes: 3
Views: 988
Reputation: 81
For people still searching for a solution to this issue, a decent workaround is to use TextWatcher in order to dynamically set/remove the hint. Keep the width and height of your EditText
at wrap_content
. The code should look like this:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
editText.setHint("My hint...");
} else {
editText.setHint("");
}
}
});
When there is no text, the hint will be set and the EditText
will resize to the dimensions of the hint. When there is some text, the hint will be empty, and so the EditText
will have to resize to the dimensions of the actual text.
Upvotes: 0
Reputation: 1803
In my opinion, the # of lines of EditText's hint should be kept one line at most by convention as you usually experience in most if not all apps out there. For instance, it'd be somewhat awkward when you have a large EditText hint that transitions to a single line on initial text input while you have other views rendered around the EditText in your layout, ya know?
Although I believe that's the reason why EditText is designed like that, there's actually an alternative way to hack and configure it the way you want it by using the TextWatcher interface for your EditText programmatically as shown in another SO post here.
P. S. Get rid of the attribute, android:background="@android:color/transparent", just so it'd make your life easier for testing :-)
Upvotes: 2