Search code examples
androidandroid-textinputlayout

How to reduce code repetition: TextInputLayout


I have just started using the TextInputLayout to enable floating hints in android. Here's how I do it:

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout_password"
    style="@style/TextInputLayout"
    android:layout_below="@+id/line1">

    <EditText
        style="@style/EditText"
        android:id="@+id/password"
        android:hint="Password"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

The problem arises here. I have to write the same TextInputLayout for all my EditText fields. I have a signup activity with 7 fields. Is there a way to implement the TextInputLayout to all the EditTexts in one go? Or will I have to write it seven times?


Solution

  • Either you have to manage all things from your Java file or you have to put it 7 times in XML.

    If you have different attribute values like inputType or hint then it will be better to use in XML or you have to manage that all things in Java

    Note: If you have same type of all TextInputLayout then you can create one common XML for it and use <include> to include in you main xml.

    Thank you.