Because of space constraints, it is difficult to create self explanatory forms in mobile apps. Do you write a label for each field? If yes, the labels will also take half of the space in your form.
Hints solve the problem to some extent. If you add a hint for an EditText, when the field is blank, this hint is shown.
So now our hint is shown in Edit Text like thisHints solve the problem to some extent. If you add a hint for an EditText, when the field is blank, this hint is shown.
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et1" android:hint="Name"/>
But the moment you start entering value into the field, the hint vanishes.
To overcome this problem, support library comes to our rescue. It has a layout called TextInputLayout which places the hint as a floating label.
To use this first you need to add two lines to your build.gradle file in yourapplication/app directory.
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.1.1' compile 'com.android.support:design:25.1.0' }
Next you should wrap your EditText in TextInputLayout as shown below.
<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et1" android:hint="Name"/> </android.support.design.widget.TextInputLayout>
Aha! Now your hint acts as a label for input field. And it is always present. When field is empty, hint is shown inside the field. When you start typing, hint is shown as floating label in accent color. And after you finish typing, hint is shown as floating label in grey color.
So you achieved all these just by using xml files (and of course support library). Happy coding!!
You can find app folder of the application here
Comments
Post a Comment