I am trying to create a "add to favorites" button.
The problem is that the drawable does not change unless I keep pressing the button. Once I release the button, it returns to the original drawable.
I followed this tutorial: https://www.youtube.com/watch?v=Nn4-Vn7qk9k but got a different result.
I created a res/drawable/custom_fav_button.xml file.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/ic_baseline_favorite_24"/>
<item
android:drawable="@drawable/ic_baseline_favorite_border_24"/>
</selector>
and I am using it in an activity as shown below.
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/custom_fav_button"/>
Thanks in advance!
As you can see in the video, your code works fine and does what you say to do. Change only while you press it. If you want to change it after click you should add in your drawable xml
drawable_button_selector.xml
drawable_button_selected.xml
drawable_button_unselected.xml
In your screen layout then you have
After that of course you have to change button state depending on your requirements. For example just switch button state on click