Code example for MenuItem

Methods: isCheckable, isChecked, setChecked

0
        final MenuItem item1 = mMenu.add(groupId, 1, 0, "item1");
        final MenuItem item2 = mMenu.add(groupId, 2, 0, "item2");
         
        // Set to exclusive 
        mMenu.setGroupCheckable(groupId, true, true);
        Assert.assertTrue("Item was not set to checkable", item1.isCheckable());
        item1.setChecked(true);
        Assert.assertTrue("Item did not get checked", item1.isChecked());
        Assert.assertFalse("Item was not unchecked due to exclusive checkable", item2.isChecked());
        mMenu.findItem(2).setChecked(true);
        Assert.assertTrue("Item did not get checked", item2.isChecked());
        Assert.assertFalse("Item was not unchecked due to exclusive checkable", item1.isChecked());
         
        // Multiple non-exlusive checkable items 
        mMenu.setGroupCheckable(groupId, true, false);
        Assert.assertTrue("Item was not set to checkable", item1.isCheckable());
        item1.setChecked(false);
        Assert.assertFalse("Item did not get unchecked", item1.isChecked());
        item1.setChecked(true);
        Assert.assertTrue("Item did not get checked", item1.isChecked());
        mMenu.findItem(2).setChecked(true);
        Assert.assertTrue("Item did not get checked", item2.isChecked());
        Assert.assertTrue("Item was unchecked when it shouldnt have been", item1.isChecked());
    } 
     
    @SmallTest 
    public void testVisibility() { 
        final MenuItem item1 = mMenu.add(0, 1, 0, "item1");