'Android selector layout of a menu item not working on a checkable item
I am trying to make a favourite item in the menu that can display a hollow heart when unchecked and a solid one when checked
However the selector approach is not working, I managed to make it work in code with a boolean value that is toggled and a item.setIcon() method. But I want to understand why my selector approach didn't work.
here's what I tried:
- I tried to make a checkable attribute in the item xml with no luck
also tried to set the setChecked directly from code checked the debugger the
item.isChecked()is returningtrueas intended but the icon doesn't change - tried to set the checked state from inside the xml to true to see if it will change icon still didn't work
The code is showing that the checking state is changing as intended but the selector is not responding by changing the icon accordingly the Question is why is that so and how to solve it.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_contact"
android:title="@string/action_contact"
android:orderInCategory="10"
app:showAsAction="never" />
<item
android:id="@+id/action_order"
android:icon="@drawable/ic_shopping_cart"
android:title="@string/action_order"
app:showAsAction="always" />
<item
android:id="@+id/action_status"
android:title="@string/action_status"
android:icon="@drawable/ic_status_info"
app:showAsAction="always" />
<item
android:id="@+id/action_favourites"
android:title="@string/action_favourites"
android:icon="@drawable/favourite_toggle_image"
android:checkable="true"
app:showAsAction="always" />
</menu>
favourite_toggle_image.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_favourite_info"
android:state_checked="true" />
<item
android:drawable="@drawable/ic_favourite_unchecked_info"
android:state_checked="false" />
</selector>
MainActivity.java
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_order:
displayToast("Order");
break;
case R.id.action_status:
displayToast("Status");
break;
case R.id.action_favourites:
lastState = !lastState;
//if (lastState) item.setIcon(R.drawable.ic_favourite_info);
//else if (!lastState) item.setIcon(R.drawable.ic_favourite_unchecked_info);
item.setChecked(lastState);
displayToast("Favourites");
break;
case R.id.action_contact:
displayToast("Contact");
break;
case R.id.action_settings:
displayToast("Settings");
break;
}
return super.onOptionsItemSelected(item);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
