I'm trying to fight the default behaviour of the Widget.AppCompat.Button.Colored style. By default the button is semi transparent. I want it to be opaque. Here is my code:
Button in layout:
<Button
android:id="@+id/search"
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/AccentButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"/>
styles.xml
<style name="AccentButton" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorAccent</item>
<item name="colorButtonNormal">#f00</item>
</style>
If I remove the style="@style/Widget.AppCompat.Button.Colored" part the button will not turn transparent if disabled.
I've tried to look into the Appcompat/Android source code. No luck though. I can't find the part where it is set to be transparent. The goal is the get a nice solution using mostly AppCompat code. I know I can create a custom drawable button backgroud. I would like to avoid it. Any ideas?
The solution is to use: <item name="android:disabledAlpha">1.0</item> like this:
<style name="AccentButton" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorAccent</item>
<item name="colorButtonNormal">#f00</item>
<item name="android:disabledAlpha">1.0</item>
</style>
How I got to this answer
After some struggle I've found btn_colored_material.xml source code.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:alpha="?attr/disabledAlpha"
android:color="?attr/colorButtonNormal" />
<item android:color="?attr/colorAccent" />
</selector>
It uses the disabledAlpha attribute. So it was only a matter of using it.
One thing I still don't get is why I have to use it with the android: prefix. I would expect that:
?android:attr/something to be set with <item name='android:something'>(...)?attr:something to be set with <item name='something'>(...)My guess is that if Android soruce code code uses it's own attributes it doesn't need the prefix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With