'Kotlin: Enable/Disable Bluetooth With Switch Material button
I want to manage the bluetooth but my layout didn't change anything when I click on the switch On/Off
Here is my layout:
<data>
<variable
name="viewmodel"
type="[...]viewmodels.BluetoothViewModel" />
<import type="android.view.View" />
</data><com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/toggle_bluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size_medium"
android:checked="@{viewmodel.state.bluetoothActivated}"
android:text="@{viewmodel.state.bluetoothActivated?@string/toggle_bluetooth_activated :@string/toggle_bluetooth_not_activated}"
In my BluetoothViewModel :
class BluetoothViewModel : BaseViewModel<BluetoothState>(BluetoothState()){
fun changeBluetoothState(bluetoothState: Boolean) = updateState {
it.copy(
bluetoothActivated = bluetoothState,
bluetoothDevices = emptyList()
)
}```
my bluetoothstate : data class BluetoothState ( var bluetoothActivated: Boolean = false,
then my fragment:
binding.toggleBluetooth.setOnCheckedChangeListener { _, state ->
bluetoothViewModel.changeBluetoothState(state)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
