I have some toggle buttons that represent the days in the week.
When a user clicks on a ToggleButton
i want it to switch states and change color, indicating to the user that it was been clicked.
This is what one of my ToggleButton
s looks like:
<ToggleButton
android:id="@+id/toggleButton_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:background="@drawable/button_border"
android:textOff="MON"
android:textOn="MON"
app:layout_constraintEnd_toStartOf="@+id/toggleButton_tuesday"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView10" />
I want the button to look like this after it's clicked (i want the same border and shape, but just different color and state changed):
The answer here doesn't work, as i already have an android:background
for my toggle buttons to show the custom border around the button.
The answer here doesn't work, as i am using a ToggleButton
and not a SwitchCompat
EDIT:
Here is the current android:background
i am setting my ToggleButton
s with:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="10dp"
/>
<solid
android:color="#FFFFFF"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="75dp"
android:height="40dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
EDIT: SOLUTION
Thank you @Broken and @Moustafa Shahin, i used a combination of their answers.
First i created two resource files for my ToggleButtons toggle_button_default.xml
and toggle_button_checked.xml
(check code above in the first EDIT). Basically, the background colors are just different in the two XMLs.
Second, i created a seletor called toggle_button_state.xml
, and loaded the appropriate resources files, that i created above:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- WHEN IS CHECKED -->
<item android:drawable="@drawable/toggle_button_default" android:state_checked="false" />
<!-- WHEN IS NOT CHECKED -->
<item android:drawable="@drawable/toggle_button_checked" android:state_checked="true" />
</selector>
And lastly, for my ToggleButtons, i set the toggle_button_state
as the background for them:
<ToggleButton
android:id="@+id/toggleButton_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/toggle_button_state"
android:textOff="MON"
android:textOn="MON"/>
In the Activity, i have an onClick
method, that i can use to monitor the clicks:
class RentActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(v: View?) {
when(v?.id){
R.id.toggleButton_monday ->{
Toast.makeText(this, "Monday Clicked", Toast.LENGTH_LONG).show()
return
}
...