Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Theming the EditText
widget, particularly when you're using the Material Design components, can significantly enhance the visual appeal and user experience of your app. Here's a step-by-step guide to theme the Material Design TextInputLayout
and TextInputEditText
in Android:
Ensure you have the Material Components library in your build.gradle
:
implementation 'com.google.android.material:material:<version>'
Replace <version>
with the appropriate library version.
Make sure your app's theme inherits from a Material Components theme, such as Theme.MaterialComponents.Light.DarkActionBar
.
To achieve a Material Design-styled EditText
, you'd typically wrap a TextInputEditText
within a TextInputLayout
:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout>
You can define a style in your res/values/styles.xml
:
<!-- Theme for TextInputLayout --> <style name="CustomTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox"> <item name="boxBackgroundColor">@color/boxBackgroundColor</item> <item name="boxStrokeColor">@color/boxStrokeColor</item> <item name="hintTextColor">@color/hintColor</item> <item name="errorTextColor">@color/errorColor</item> <item name="boxStrokeErrorColor">@color/boxErrorColor</item> </style> <!-- Theme for TextInputEditText --> <style name="CustomEditText" parent="Widget.MaterialComponents.TextInputEditText.FilledBox"> <item name="android:textColor">@color/textColor</item> </style>
Apply these styles in your XML layout:
<com.google.android.material.textfield.TextInputLayout style="@style/CustomTextInputLayout" ...> <com.google.android.material.textfield.TextInputEditText style="@style/CustomEditText" ... /> </com.google.android.material.textfield.TextInputLayout>
val textInputLayout: TextInputLayout = findViewById(R.id.textInputLayout) val editText: TextInputEditText = findViewById(R.id.editText) textInputLayout.boxBackgroundColor = ContextCompat.getColor(this, R.color.boxBackgroundColor) textInputLayout.boxStrokeColor = ContextCompat.getColor(this, R.color.boxStrokeColor) textInputLayout.defaultHintTextColor = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.hintColor)) editText.setTextColor(ContextCompat.getColor(this, R.color.textColor))
These are the basic steps to theme the TextInputLayout
and TextInputEditText
in your Android app. Remember, Material Components offer a lot of attributes for customizing the appearance of these widgets. Always refer to the official documentation for the most up-to-date theming guidance and comprehensive attribute lists.
Customizing Material Design EditText appearance in Android:
<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="Custom EditText" /> </com.google.android.material.textfield.TextInputLayout>
Changing color and style of Material Design EditText in Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Styled EditText" /> </com.google.android.material.textfield.TextInputLayout>
Material Design theming for EditText in Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Themed EditText" /> </com.google.android.material.textfield.TextInputLayout>
Using styles and themes with Material Design EditText Android:
<style name="MyEditTextStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="boxStrokeColor">@color/customEditTextStrokeColor</item> <item name="android:textColorHint">@color/customEditTextHintColor</item> <!-- Add other style attributes as needed --> </style>
<com.google.android.material.textfield.TextInputLayout style="@style/MyEditTextStyle" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Styled EditText" /> </com.google.android.material.textfield.TextInputLayout>
Adding custom icons and text to Material Design EditText with theming:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Customized EditText" android:drawableStart="@drawable/ic_custom_icon" /> </com.google.android.material.textfield.TextInputLayout>
Material Design EditText animations and theming in Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Animated EditText" /> </com.google.android.material.textfield.TextInputLayout>
Themed Material Design EditText with XML layout in Android:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Themed EditText" /> </com.google.android.material.textfield.TextInputLayout>
Applying custom colors to Material Design EditText Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Colored EditText" /> </com.google.android.material.textfield.TextInputLayout>
Material Design EditText theming in XML layout in Android:
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Themed EditText" /> </com.google.android.material.textfield.TextInputLayout>
Changing size and elevation of Material Design EditText with theming:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="200dp" android:layout_height="48dp" android:hint="Sized EditText" /> </com.google.android.material.textfield.TextInputLayout>
Material Design EditText states and theming in Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Stateful EditText" android:stateListAnimator="@anim/customEditTextStateListAnimator" /> </com.google.android.material.textfield.TextInputLayout>
Creating themed Material Design EditText with Kotlin in Android:
val editText = TextInputEditText(context) val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) editText.layoutParams = layoutParams editText.hint = "Kotlin Themed EditText" editText.setBoxStrokeColorResource(R.color.customEditTextStrokeColor) editText.setHintTextColor(ContextCompat.getColor(context, R.color.customEditTextHintColor))
Material Design outlined and filled EditText theming in Android:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Outlined EditText" android:inputType="text"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/customEditTextStrokeColor" app:hintTextColor="@color/customEditTextHintColor"> <com.google.android.material.textfield.TextInputEditText style="@style/Widget.MaterialComponents.TextInputEditText.FilledBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Filled EditText" android:inputType="text"/> </com.google.android.material.textfield.TextInputLayout>