Issue
I've always been struggling with changing/overwriting default styles. I am using the new material design library for my widgets. For example, I'm overwriting my MaterialButton style:
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
...
<item name="materialButtonStyle">@style/CustomButton</item>
</style>
...
...
<style name="CustomButton" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">@color/colorTextPrimary</item>
<item name="android:backgroundTint">@color/button_background</item>
</style>
Where the background color is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="@color/colorAccent"/>
<item android:state_enabled="false" android:color="@color/colorDisabled"/>
</selector>
The problem is that this way I cannot set the alpha for the disabled color as it only affects the background color and not the textColor... Another issue with this approach is that the MaterialDesign.Text button (Widget.MaterialComponents.Button.TextButton) also gets overwritten and breaks...
I've got very similar issues with other widgets like TextView, InputFields... What is the proper way of overwriting different styles without breaking anything? I've always considered of extending these classes (like MaterialButton) and change things up programmatically, but it just feels wrong to me, not the conventional way... am I wrong?
Solution
You can use something like:
<style name="CustomButton" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">@color/color_selector</item>
<item name="backgroundTint">@color/button_background</item>
</style>
where color_selector is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorOnPrimary" android:state_enabled="true"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
and button_background
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.