EditText is a standard widget in Android, used for user input. With Material Design, EditText received a visual update to align with the new design principles. This version of EditText is part of the Material Components library and provides more consistent styling, animations, and behavior.
Here's how you can use and customize Material Design's EditText (referred to as TextInputEditText when using Material Components):
Ensure you have the Material Components library added to your app's build.gradle:
implementation 'com.google.android.material:material:1.4.0'
TextInputLayout and TextInputEditText in Your Layout:For Material-styled EditText, you should wrap TextInputEditText inside a TextInputLayout.
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Name"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
If you want the "outlined box" style (a box around the EditText), you can set the boxBackgroundMode attribute:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Name" app:boxBackgroundMode="outline"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" app:helperText="At least 8 characters" app:errorTextAppearance="@style/ErrorText"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
In your Kotlin/Java code:
val textInputLayout: TextInputLayout = findViewById(R.id.textInputLayout) textInputLayout.error = "Password is too short"
You can add end icons such as clear text button or password toggle button:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
You can add a counter and set a max character limit:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Bio" app:counterEnabled="true" app:counterMaxLength="150"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
There are numerous attributes available for customization, including app:boxStrokeColor, app:boxStrokeWidth, app:hintTextAppearance, and many more.
By incorporating the TextInputLayout and TextInputEditText widgets from the Material Components library into your Android app, you can achieve a modern and consistent design that aligns with Material Design principles. Always ensure that any customizations maintain a clear and user-friendly experience.
Creating a Material Design TextInputLayout in Android:
EditText in a TextInputLayout to enable Material Design features.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Android Material Design EditText example code:
TextInputLayout and TextInputEditText to create a Material Design EditText.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Working with floating labels in Material Design EditText:
EditText when users start typing.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:hintEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Customizing appearance of Material Design EditText:
app:boxStrokeColor, app:boxBackgroundColor, and others.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customColor" app:boxBackgroundColor="@color/customBackground"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Error handling in Material Design EditText in Android:
setError method.textInputLayout.setError("This is an error"); <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Using outlined boxes in Material Design EditText:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox".<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Accessibility features in Material Design EditText:
android:importantForAccessibility attribute for accessibility.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:importantForAccessibility="yes"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
Material Design EditText XML layout examples in Android:
EditText layouts in XML with various attributes.<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout>
periodicity e2e-testing textinputlayout flutter-dependencies venn-diagram uml browser-tab image-processing android-constraintlayout shiny-server