Home » Android » Android | Change theme of TextInputLayout using styles.xml

Android | Change theme of TextInputLayout using styles.xml

Posted by: admin June 15, 2020 Leave a comment


I want to apply custom style to TextInputLayout's hint and error in one theme and apply it globally i.e. define it in styles.xml and somehow apply it to all TextInputLayouts used throughout the app without having the need to add it inline like this:


<style name="my_custom_style" parent="Widget.Design.TextInputLayout">
    <item name="android:textColorHint">@color/success_accent</item>
    <item name="android:textSize">20sp</item>

Something like we can do the Button widget like this:


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="buttonStyle">@style/my.Widget.Button</item>
    <item name="android:buttonStyle">@style/my.Widget.Button</item>

<style name="my.Widget.Button" parent="@android:style/Widget.TextView">
    <item name="android:gravity">center</item>
    <item name="android:textSize">@dimen/some_dimen</item>
    <item name="android:textAllCaps">false</item>

NOTE: I am currently considering subclassing the TextInputLayout as a last resort, so, please keep this in mind while you answer.

Thanks 🙂

How to&Answers:

Unfortunately, the TextInputLayout widget, and it seems all widgets in the Design Support Library, don’t define a global theme attribute for it’s default style. Therefore, it’s not possible to customize it’s style globally, other than by subclassing it to support a custom theme attribute to query the default style from, and using the subclass everywhere instead.

Note that the Widget.Design.TextInputLayout style would still be hardcoded as the default style on which the widget would fall back to if it can’t find the attribute defined in it’s theme, so this would be no different than the existing implementation by default.

It seems that there is a misconception in the Design Support Library developers that defining a default theme attribute requires it to be present in the current theme in order to work properly. This issue was reported previously for TabLayout, but was closed based on this reasoning, and subsequent queries and clarifications did not generate further response. Feel free to open another ticket on the AOSP issue tracker with the necessary clarification; hopefully it might fare better.


I’m using this actually

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    [...]// other definitions
    <item name="editTextStyle">@style/EditTextDefault</item>

 <style name="Widget.Design.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
     <item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
     <item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>

<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
     <item name="android:textColor">@color/colorAccent</item>

 <style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
     <item name="android:textColor">#d32f2f</item>

<style name="EditTextDefault" parent="android:Widget.EditText">
    <item name="android:paddingLeft">10dp</item>


Just thinking out loud here, did you try this? This should change the color hint all over your app, but if you are want this anyways this just might help with your case.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorHint">@style/text_hint_appearance</item>

<style name="text_hint_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColorHint">@color/success_accent</item>