How to programmatically change the default value for a toggle button

I have a toggle button with the following features:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/ic_heart_pressed"/> 
    <item android:state_pressed="true" android:drawable="@drawable/ic_heart"/> 
    <item android:state_checked="true" android:drawable="@drawable/ic_heart_pressed"/> 
    <item android:drawable="@drawable/ic_heart"/> 
</selector>

It works great. However, since this is a favorite button, when I present the screen a second time, and it contains some elements that are favorites to begin with, I want to change the default value from hearttoheart_pressed

I tried to do this programmatically, like this, in getViewmine ArrayAdapter, but with this I lose the ability to "switch" the button.

if (item.isFav())  
         holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart_pressed));
else
         holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart));

Question

Is there a way to change the default ToggleButton programmatically based on a condition in the code?

+3
source share

No one has answered this question yet.


All Articles