lundi 27 mai 2019

Can you put a checkbox in a switch statement for onOptionsItemSelected?

I followed part of the last example here: How to Add a CheckBox in Toolbar with custom background - android to add a custom star as a checkbox in my toolbar.

However, I would like to put the checkbox inside the switch statement of onOptionsItemSelected, but any code inside a switch statement like R.id.star_favorite is not called. Is there a better way to call updateFavorite() and still be able to use my custom star checkbox? @Ramtin

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    checkBox = (CheckBox) menu.findItem(R.id.star_favorite).getActionView();
    checkBox.setButtonDrawable(R.drawable.favorite_checkbox);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("onClick", "favorite button has been clicked");
            currentQuoteIsFavorite = !currentQuoteIsFavorite;
            updateFavorite(currentQuoteIsFavorite);
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.star_favorite:
            //already tried putting code like updateFavorite() inside here but it's not called
        case R.id.share_quote:
            Log.e("INSIDE SHARE", currentQuote);
            shareQuote();
            break;
        case R.id.menu:
            break;
    }
    return super.onOptionsItemSelected(item);
}




Aucun commentaire:

Enregistrer un commentaire