mardi 5 mai 2020

Snackbar widget usage in android with checkbox

I want to use snackbar to show a message when a certain checkbox is marked.I saw various sources and found this.Is it right?

CheckBox WhippedCreamCheckBox = (CheckBox) findViewById(R.id.whip_check);
        final boolean hasWhippedCream = WhippedCreamCheckBox.isChecked();

This is the checkbox and the condition i made to enable snackbar is this:-

if(hasWhippedCream){
            WhippedCreamCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Snackbar whipsnack=Snackbar.make(v,"$1 charged for Whip Cream",Snackbar.LENGTH_SHORT);
                    whipsnack.show();
                    }
            });
            extra=1;
        }

(Ignore extra) And if it's right then it is showing error during import of class.It says this class doesnot exist(It added it itself).Import:-

import com.google.android.material.snackbar.Snackbar;

Please help me. And also if you can please also tell how we can add an undo option and activate it to unmark a checkbox.

Aucun commentaire:

Enregistrer un commentaire