Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Nougat: Why do checkboxes on Fragment have incomplete state when selected programmatically (but look fine on Lollipop)

Here is what my settings tab (Fragment) looks like before any selections are made:

no selections

From the main Fragment the user can make a selection from the Spinner -- looks like the following (after the user has made a choice):

super is selected

When the user makes that selection, choices the user has previously chosen which are saved in the User Prefs are loaded and the appropriate checkboxes are selected. Checkboxes which were shown in first snapshot now look like the following:

partially selected

See, the items which now have associated checkboxes set to pink? Those items turn pink but the check is not shown for some reason on Nougat. Those items are actually selected, because if I click them once then they then become unselected.

Lollipop Works As Expected However, if I run the program on Lollipop, the items show up completely selected (with checkboxes checked) as expected.

Manually Setting Them On Nougat Works Also, if I manually set them (click them while the app is running) on Nougat then it works as expected for all other items chosen in the spinner while the app continues to run.

checkboxes are selected as expected

Here's the basics of what is going on for those to get set:

When the item is selected in the Spinner, I get the rootview and call findViewById()

  addCharsTabCheckBox = (CheckBox)rootView.findViewById(R.id.addCharsTabCheckBox);
     maxLengthTabCheckBox = (CheckBox)rootView.findViewById(R.id.maxLengthTabCheckBox);

After that I just call the setChecked() on each CheckBox:

addCharsTabCheckBox.setChecked(currentSiteKey.isHasSpecialChars());

Would you happen to know why this might be happening?

Have you seen this partial behavior before?

Edit #1 While running I also found that unselected items will show up as if they are partially selected (checkboxes but gray) and look like the following:

unselected but checked

Those gray items are not actually selected. Also, if I change the device orientation while viewing that Settings fragment then the checkboxes get redrawn (obviously) and show up unchecked as expected.

Edit #2

Just tested on Marshmallow and it does work properly (or as expected) on that API level also. marshmallow works also


EDIT 3

I have created a simplified project based upon the TabPage template provided by Android Studio 2.3.3 in order to study this problem more closely.

Public GitHub Project

I've added the code to a public GitHub repo so you can get it and try it: https://github.com/raddevus/CheckBoxFragTest

The project is a bit different than the one above but they are both based upon that template provided by Android Studio 2.3.3. The test project includes 3 tabs so I could investigate whether or not I'd see the same odd behavior on all tab pages. SPOILER : I did not.

Here's what the app looks like from he first tab page:

tab page 1

The Two Most Interesting Parts

  1. When you click the [Toggle Checkbox] button on Tab Page 1, it will call setChecked on two checkboxes (1 checkbox on tabPage2 and and 1 checkbox on tabPage3)

  2. When you click the button on TabPage3, it will set the checkbox shown on tabPage1 (seen above in the image).

The Problem

On Nougat, when you click the [Toggle Checkbox] button on TabPage1, it sets the checkbox on tabPage2 to the odd state, we saw in the original problem report. However, the checkbox on tabPage3 is set and fully checked as you expect.

EDIT 4

Oreo Android v8.0 API Level 26

I have more data. I just ran the app on Oreo (emulator) and I see the same issue there. Here's the version I ran on:

oreo version

Here's what it looks like the first time I click the [Toggle Checkbox] button.

partially-selected state Oreo

But, notice that the checkbox on tabpage3 is actually selected properly even though it is set from the same static boolean:

tabpage3 checkbox - selected

If you click the [Toggle Checkbox] button a second time then you get the odd grey checkbox state (which is actually unselected.

partially-selected unselected

However, the checkbox on tabpage3 is actually unselected as it should be.

And now, if you click the [Toggle Checkbox] again, then it will select the checkbox on tabPage2 properly and will work properly as long as the app runs after this.

like image 669
raddevus Avatar asked Oct 01 '17 20:10

raddevus


1 Answers

just Call this jumpDrawablesToCurrentState() after calling radioButton.setChecked(true) or checkBox.setChecked(true);

Code which worked for me in my Case


CheckBox checkBox   = (CheckBox) clView;   
checkBox.setChecked(true);
checkBox.jumpDrawablesToCurrentState();

RadioButton radioButton = (RadioButton)  clView; 
radioButton.setChecked(true);
radioButton.jumpDrawablesToCurrentState();
like image 150
Ramesh kumar Avatar answered Nov 03 '22 08:11

Ramesh kumar