I am developing an app in Android Studio.
I have a Switch to enable / disable some functionality.
I am using an xml file to define the colour of the thumb and track – both gray when switch is disabled (thumb to the left), and both green when the switch is enabled (thumb to the right).
This works fine.
However, I also need to update the switch display when a message is received from a remote Bluetooth device. For example, if the switch is in the disabled position (gray), and an enable message is received, then I want to update the switch display so that it is enabled (tall green, thumb to the right).
Right now, when I try to update the switch display, the switch thumb changes color to green (= desired behavior), but
(a) the thumb stays to the left, and (b) the track stays gray.
My code is below. Can anyone see what I am doing wrong?
Thanks in advance
Garrett
Switch definition:
<Switch
android:id="@+id/switch_1_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:thumbTint="@drawable/switch_selector"
android:trackTint="@drawable/switch_selector"
android:text=""
/>
When the switch is selected/pressed by the user:
switch_1_select.setOnClickListener {
if (switch_1_select.isChecked)
{
// do something
} else {
// do something else
}
}
XML file to (supposedly) control switch display:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorGreen" android:state_checked="true" />
<item android:color="@color/colorGray"/>
</selector>
Code called when enable message received from remote device:
switch_1_select.isChecked = true
In pictures....
Here is a pic showing the switch in the disabled position (with thumb to the left, and both thumb and track gray).
Here is a pic showing the switch in the enabled position (with thumb to the right, and both thumb and track green). This is how the switch looks when the user clicks on it.
And finally, here is a pic showing the switch when I programmatically try to enable it (from its disabled position). Notice that although the thumb is green, the thumb remains to the left, and the track color remains gray.
Edit / Update:
I updated my switches to SwitchCompat, after reading this post: How to change the color of a SwitchCompat from AppCompat library
Switch definition:
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_1_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
/>
So I no longer use an XML style file to try and control the thumb / track colors. Instead, in my app theme, I define the colors:
<!-- colorControlActivated is the color applied to framework controls
in their activated (ex. checked, switch on) state. -->
<item name="colorControlActivated">@color/colorGreen</item>
<!-- colorSwitchThumbNormal is color applied to framework switch
thumbs in their normal state. (switch off). -->
<item name="colorSwitchThumbNormal">@color/colorGray</item>
Result:
Some improvement
When I programmatically check / uncheck the switch, the thumb and track colours change (gray for unchecked, green for checked).
However the thumb position does NOT change.
To check / uncheck the switch programmatically, I have tried:
switch_1_select.toggle()
and
switch_1_select.isChecked = false
switch_1_select.isChecked = true
It looks like the code might need to manually set the thumb position
I tried this:
switch_cooler_status_select.setThumbPosition(0.0F)
switch_cooler_status_select.setThumbPosition(1.0F)
but I get the following error:
Cannot access 'setThumbPosition': it is package-private in 'SwitchCompat'