Search code examples
androidbuttonselector

Strange button behavior XML selector enabled


I have a button that must be enabled/disabled dynamically. When the switch on the right is checked, the button must be enabled. When the switch is unchecked, the button must be disabled. I've done that using Bindings in MVVMlight.

enter image description here

The default state of the switch is unchecked, so the button must be disabled.

The button has it's own Color State List to be grayed when disabled and blue when enabled.

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_enabled="false" android:color="@color/third_light_gray"/>
   <item android:state_enabled="true" android:color="@color/light_blue"/>
</selector>

The binding works properly, the button gets enabled/disabled as expected. However, the default state of the button should be disabled, but it is always enabled by default.

<Button
        android:id="@+id/buttonListProtections"
        ...
        android:enabled="false"
        android:theme="@style/Gohy.Button" />

Button style :

  <style name="Gohy.Button" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textColor">@color/white</item>
    <item name="colorButtonNormal">@drawable/button_color_list</item>
    <item name="android:padding">30dp</item>
    </style>

What have I done wrong ?


Solution

  • I have found an answer to my question via this post :

    Android custom view ignoring `android:enabled` in XML?

    Since there is apparently no way to enable/disable view directly from XML, I had to set it programmatically.