In one of my App's fragments the layout uses a toggle button defined as:
<ToggleButton
android:id="@+id/custom_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="4dp"
android:minWidth="110dp"
android:minHeight="36dp"
android:background="@drawable/toggle_button_bg_bw"
android:textColor="@drawable/toggle_color_bw"
android:textOff="@string/text_off"
android:textOn="@string/text_on"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/stm"
app:layout_constraintTop_toTopOf="parent" />
Background drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/black_button_selected"
android:state_checked="true" />
<item
android:drawable="@drawable/white_button_unselected" />
</selector>
black_button_selected:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="@color/colorGreen" />
<solid
android:color="@color/colorBlack" />
<corners android:radius="3dp" />
</shape>
white_button_unselected:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="@color/colorGreen" />
<solid
android:color="@color/colorWhite" />
<corners android:radius="3dp" />
</shape>
TextColor drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:color="@color/colorBlack" />
<item
android:state_checked="true"
android:color="@color/colorWhite" />
</selector>
In createView following code is called. options_view contains other buttons and view elements, which is attached to editorView and editorView is a part of this fragment layout.
optionsViewBinding = DataBindingUtil.inflate(
inflater,
R.layout.options_view,
binding.editorView,
true
)
optionsViewBinding.run {
customToggleButton.isChecked = false
customToggleButton.setOnClickListener {
someFunction(customToggleButton.isChecked)
}
}
Problem I am facing is, when I toggle the switch to 'on' and move on to some other fragment and come back to fragment with this toggle button, while the state of the toggle button has reset to 'false'/OFF state and yet the view on display shows it in its 'on' state.
What am I missing here?