vendredi 18 février 2022

Android checkbox.isChecked is not working

I am trying to create a like and dislike button. I use Checkboxes to do so.

In the XML code I have two checkboxes one called like and the other dislike

I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.

public void onLike(View view) {
        if (dislike.isChecked()) {
            dislike.setChecked(false);
        }
        Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
    }

The issue that I am having is that set setChecked(true) is not doing anything.

For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.

the way I initialized the checkbox in the main activity is as follows: -

View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
        like = (CheckBox) cardViewLayout.findViewById(R.id.like);
        dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);

any ideas what's going on?




Aucun commentaire:

Enregistrer un commentaire