I want the size, font, and color of the text to match the size of the TextInputLayout floating label .
This can be easily achieved without any external libraries. After trying to hack into TextInputLayout and even creating my own custom view, I realized that using a simple TextView takes much less code and is probably more efficient.
The text style can be copied from the AppCompat library.
Style
In the Material Design Guide, we get the following information:
- label must have a lower field of
8dp - the label should be aligned vertically with the input text
This guide does not mention EditText materials:
- he has a
4dp left 4dp - There is actually no
16dp space on his label, this remains for the interface designer: it makes sense, because if you put it under another EditText , you only need an extra 8dp space
In addition, the design support library contains this style for the focus element label:
<style name="TextAppearance.Design.Hint" parent="TextAppearance.AppCompat.Caption"> <item name="android:textColor">?attr/colorControlActivated</item> </style>
Inactive elements simply use TextAppearance.AppCompat.Caption .
Implementation
Add the following to your dimens.xml :
<dimen name="input_label_vertical_spacing">8dp</dimen> <dimen name="input_label_horizontal_spacing">4dp</dimen>
Then add this to styles.xml :
<style name="InputLabel" parent="TextAppearance.AppCompat.Caption"> <item name="android:paddingBottom">@dimen/input_label_vertical_spacing</item> <item name="android:paddingLeft">@dimen/input_label_horizontal_spacing</item> <item name="android:paddingRight">@dimen/input_label_horizontal_spacing</item> </style>
If you want the label to always have a highlighted (accent) color, replace TextAppearance.AppCompat.Caption with TextAppearance.Design.Hint in the Google Design Support Library. However, this is likely to be a little strange if you also marked EditText views on the same screen.
Finally, you can put a TextView over your Spinner (or any other element) with the applicable style:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/category" style="@style/InputLabel" />
Result
The following screenshot shows a simple example with two normal TextInputLayout views, followed by a label, and Spinner . I did not use the extra 8dp spacing to separate them further, but this shows that the size, font and color are reflected.
The elements inside Spinner have different additions, however, I prefer to maintain vertical alignment with all other labels to get a more even look.
