Aviel Fedida
Aviel Fedida

Reputation: 4102

setChecked(false) is the same as setChecked(true)?

I'm using android:checkableBehavior="single" within a group, this group contains few items, those items represents content filters, there is no default filter(meaning there is not always has to be a checked item, at least that what I wants), if I click a filter and want to disable it I can click it again and my expectation is that by using setChecked(false) the item will unchecked.

However, it looks like:

For checkableBehavior="single" setChecked() will always check the menu item even if parameter is 'false' because of Google implementation.

My obvious solution is adding a no filter item that can be checked by the users to indicates they don't want a filter but it's just seems more intuitive to check and uncheck the same item, it there another way to setChecked(false)?

Upvotes: 2

Views: 1180

Answers (1)

Anm
Anm

Reputation: 3361

Google's brain-dead implementation in MenuItemImpl (as of Nougat):

@Override
public MenuItem setChecked(boolean checked) {
    if ((mFlags & EXCLUSIVE) != 0) {
        // Call the method on the Menu since it knows about the others in this
        // exclusive checkable group
        mMenu.setExclusiveItemChecked(this);
    } else {
        setCheckedInt(checked);
    }

    return this;
}

Note that checked is completely ignored when the EXCLUSIVE flag is set.

Upvotes: 3

Related Questions