Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox value is true but tick is not showing - Android / Java

Basically here I have three(3) checkbox list in three(3) different fragment. When I open the activity, my checkbox in the default fragment display like normal, but the one in different fragment display differently. The value of the checkbox is still true. Below is the image for the checkbox to make things clearer and also my code.

First fragment page:

enter image description here

Second fragment page:

enter image description here

Code for setting up the checkbox and its if condition

private void setCheckBox(ArrayList<DataPrefType> arrayList){
    for(int i = 0; i < arrayList.size(); i++){
        id = i + 1;
        checkBox = new CheckBox(getActivity());
        checkBox.setId(i + 1);
        checkBox.setText(arrayList.get(i).getName());
        checkBox.setPadding(10, 10, 10, 10);
        checkBox.setLayoutParams(params);
        checkBox.setGravity(Gravity.CENTER);
        checkBoxLayout.addView(checkBox);

        if(!userPreference.isEmpty() || userPreference != null){
            for(int j = 0; j < userPreference.get(0).getDataPrefTypeArrayList().size(); j++){
                int retrieveId = Integer.parseInt(userPreference.get(0).getDataPrefTypeArrayList().get(j).getId());
                if(checkBox.getId() == retrieveId)
                    checkBox.setChecked(true);
            }
        }
like image 650
azriebakri Avatar asked May 30 '17 05:05

azriebakri


People also ask

How do I know if my checkbox is clicked Android?

So, the method to know if the check box is checked is : (CheckBox) yourCheckBox. isChecked() it returns true if the check box is checked.

How do you check if a checkbox is checked or not in Java?

JCheckBox often uses the methods isSelected and setSelected. To select, or un-select, a box, use the setSelected(boolean) method. To check if a box is selected, use the isSelected() method.

What is the way to get the checked status of checkbox in Android?

You can call isChecked() on a checkbox to get its status. If you want to count the number of checked checkboxes you could store then in an ArrayList and loop through that.

How do I stop a checkbox from being unchecked Android?

Just add the android:clickable="false" attribute in the layout xml.


2 Answers

The bug with the checkboxes not showing as checked even though isChecked is true is within the animation of the draw.

I found this solution here. I am posting it here as an answer to make it easier to find and including some extra information about how to call it.

Try calling jumpDrawablesToCurrentState on the root view (or parent view of your check boxes). If specifically calling to checkBox.setChecked(true); as OP is, call this after. In this question, it would be important to call this after all checkboxes have been added to the parent view. This essentially will draw all drawables in the current state, skipping the animation process.

Example:

@Override
public void onStart() {
    View v = getView();
    if(v != null) {
        CheckBox chkYourCheckBox = (CheckBox)v.findViewById(R.id.chkYourCheckBox);
        chkYourCheckBox.setChecked(true); //sometimes this may draw fine, and other times it may not
        //***The important line:
        v.jumpDrawablesToCurrentState();
    }
}
like image 79
Mira_Cole Avatar answered Oct 25 '22 14:10

Mira_Cole


You can override the Fragment's setUserVisibleHint() method and load in the checkboxes there.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser)setCheckBox();
}

This is just a workaround, and I don't think its the best way to solve it, but I haven't found anything better yet. This can lead to Null pointer exceptions.

like image 28
xSooDx Avatar answered Oct 25 '22 15:10

xSooDx