Reputation: 5131
Does API 21 provide a method to use the following feature:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
I'm trying to float the EditText hints.
Thanks!
Upvotes: 102
Views: 151424
Reputation: 363469
Use the TextInputLayout
provided by the Material Components Library:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
Upvotes: 0
Reputation: 270
You need to add the following to your module build.gradle file:
implementation 'com.google.android.material:material:1.0.0'
And use com.google.android.material.textfield.TextInputLayout in your XML:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/my_hint">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</com.google.android.material.textfield.TextInputLayout>
Upvotes: 8
Reputation: 1010
For an easier way to use the InputTextLayout, I have created this library that cuts your XML code to less than the half, and also provides you with the ability to set an error message as well as a hint message and an easy way to do your validations. https://github.com/TeleClinic/SmartEditText
Simply add
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
Then you can do something like this:
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/emailSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Email"
app:setMandatoryErrorMsg="Mandatory field"
app:setRegexErrorMsg="Wrong email format"
app:setRegexType="EMAIL_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/passwordSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Password"
app:setMandatoryErrorMsg="Mandatory field"
app:setPasswordField="true"
app:setRegexErrorMsg="Weak password"
app:setRegexType="MEDIUM_PASSWORD_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/ageSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Age"
app:setMandatory="false"
app:setRegexErrorMsg="Is that really your age :D?"
app:setRegexString=".*\\d.*" />
Upvotes: 0
Reputation: 7394
Import the Support Libraries, In your project's build.gradle file, add the following lines in the project's dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
Use following TextInputLayout in your UI Layout:
<android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username"/>
</android.support.design.widget.TextInputLayout>
Than, call setHint on TextInputLayout just after setContentView call because, to animate the floating label, you just need to set a hint, using the setHint method.
final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
usernameWrapper.setHint("Username");
Upvotes: 9
Reputation: 1462
Floating hint EditText:
Add below dependency in gradle:
compile 'com.android.support:design:22.2.0'
In layout:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</android.support.design.widget.TextInputLayout>
Upvotes: 109
Reputation: 1893
The Android support library can be imported within gradle in the dependencies :
compile 'com.android.support:design:22.2.0'
It should be included within GradlePlease! And as an example to use it:
<android.support.design.widget.TextInputLayout
android:id="@+id/to_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextViewTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="To"
android:layout_marginTop="45dp"
/>
</android.support.design.widget.TextInputLayout>
Btw, the editor may not understand that AutoCompleteTextView is allowed within TextInputLayout.
Upvotes: 18
Reputation: 4624
Yes, as of May 29, 2015 this functionality is now provided in the Android Design Support Library
This library includes support for
Upvotes: 60
Reputation: 690
Android hasn't provided a native method. Nor the AppCompat.
Try this library: https://github.com/rengwuxian/MaterialEditText
This might be what you want.
Upvotes: 12
Reputation: 19416
No it doesn't. I would expect this in a future api release, but for now we are stuck with EditText. Another option is this library:
https://github.com/marvinlabs/android-floatinglabel-widgets
Upvotes: 0
Reputation: 9081
@andruboy's suggestion of https://gist.github.com/chrisbanes/11247418 is probably your best bet.
https://github.com/thebnich/FloatingHintEditText kind of works with appcompat-v7 v21.0.0, but since v21.0.0 does not support accent colors with subclasses of EditText
, the underline of the FloatingHintEditText
will be the default solid black or white. Also the padding is not optimized for the Material style EditText
, so you may need to adjust it.
Upvotes: 3