Material Design EditText in Android with Examples

Material Design EditText in Android with Examples

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):

1. Add the Dependency:

Ensure you have the Material Components library added to your app's build.gradle:

implementation 'com.google.android.material:material:1.4.0' 

2. Use 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> 

3. Outlined Box Style:

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> 

4. Adding Helper Text and Error Message:

<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" 

5. Adding End Icons:

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> 

6. Counter and Limit:

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> 

7. Customization:

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.

Examples

  1. Creating a Material Design TextInputLayout in Android:

    • Wrap your 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> 
  2. Android Material Design EditText example code:

    • Use 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> 
  3. Working with floating labels in Material Design EditText:

    • Floating labels appear above the 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> 
  4. Customizing appearance of Material Design EditText:

    • Customize appearance using attributes like 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> 
  5. Error handling in Material Design EditText in Android:

    • Show errors using 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> 
  6. Using outlined boxes in Material Design EditText:

    • Enable outlined boxes using 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> 
  7. Accessibility features in Material Design EditText:

    • Use 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> 
  8. Material Design EditText XML layout examples in Android:

    • Define Material Design 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> 

More Tags

periodicity e2e-testing textinputlayout flutter-dependencies venn-diagram uml browser-tab image-processing android-constraintlayout shiny-server

More Programming Guides

Other Guides

More Programming Examples