I’m developing a wizard UI in which the user can go ahead only if the form matches some criteria.
So far, I have successfully developed all the logic behind the buttons, and the layout of the buttons themselves, so I can enable the forward button only when the user inserted some information.
<Button
android:id="@+id/btnForward"
style="@style/buttonForward_disabled"
android:enabled="false" />
As you can see I used style attributes to describe when a button (which is technically a button with background drawable and text)
My styles.xml file contains the following
<style name="buttonFB">
<item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
<item name="android:textColor">#ffffff</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_weight">0.4</item>
<item name="android:layout_marginBottom">25dp</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:enabled">true</item>
</style>
<style name="buttonFB_disabled" parent="buttonFB">
<item name="android:textColor">#ffffff</item>
<item name="android:enabled">false</item>
</style>
<style name="buttonForward_disabled" parent="buttonFB_disabled">
<item name="android:background">@drawable/btnfw_disabled</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_marginRight">14dp</item>
<item name="android:text">@string/Forward</item>
</style>
<style name="buttonBackward_disabled" parent="buttonFB_disabled">
<item name="android:background">@drawable/btnfw_disabled</item>
<item name="android:layout_alignParentLeft">true</item>
<item name="android:layout_marginLeft">14dp</item>
<item name="android:text">@string/Backward</item>
</style>
and buttons display correctly when they are instantiated into the layout. The obvious problem is that Button.setEnabled(true) only makes the button clickable but doesn’t change the layout at all. In the example above you see that in both cases the text is white: it’s expected to change in the immediate future as soon as I review all the graphics.
I have read that I cannot programmatically change the style of buttons at runtime. What can I do to make the buttons look different according to their enabled state?
Use selectors. They are meant to do exactly what you want. Basically they define different styles to be applied to your button in accordance of the select, focused and enabled state.