0

I'm adding a badge to an icon in Toolbar. I used onCreateOptionMenu to inflate the icon in toolbar. I need to used the Menu on a button click to use LayerDrawable for adding badge to the icon. I found that you can access Menu like this. But when I use Menu like this, the icon is not updated its recreated again in such way that there becomes 2 icons. One has a badge the other one does not. If I click on the button again, 3 icons are shown. Is there any way to do it properly?

public class ProdDetailsActivity extends AppCompatActivity {

    private SQLiteHandler db;
    private MenuItem itemCart;
    private Menu menu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proddetails);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }   

    public void addcart(View view){
        updatecart();
    }

    public void updatecart(){
        //Getting current count frm database
        HashMap<String, String> pro = db.getCartCount();
        String count = pro.get("cartc");
        if(TextUtils.isEmpty(count)) {
            getMenuInflater().inflate(R.menu.cart_menu, menu);
            MenuItem itemCart = menu.findItem(action_cart);
            LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
            setBadgeCount(ProdDetailsActivity.this, icon, "1");
            db.addCart("1");
        } else {
            int cc = Integer.parseInt(count);
            String finalcount = String.valueOf(cc+1);
            Log.e("sammy_finalcount ",finalcount);
            getMenuInflater().inflate(R.menu.cart_menu, menu);
            MenuItem itemCart = menu.findItem(action_cart);
            LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
            setBadgeCount(ProdDetailsActivity.this, icon, finalcount);
            db.addCart(finalcount);
        }
    }

    public void setBadgeCount(Context context, LayerDrawable icon, String count) {
        Log.e("sammy_count ",count);
        BadgeDrawable badge;
        Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
        if (reuse != null && reuse instanceof BadgeDrawable) {
            badge = (BadgeDrawable) reuse;
        } else {
            badge = new BadgeDrawable(context);
        }

        badge.setCount(count);
        icon.mutate();
        icon.setDrawableByLayerId(R.id.ic_badge, badge);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updatecart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        //Accessing Menu here
        this.menu=menu;
        getMenuInflater().inflate(R.menu.cart_menu, menu);

        MenuItem itemCart = menu.findItem(action_cart);
        LayerDrawable icon = (LayerDrawable) itemCart.getIcon();

        setBadgeCount(this,icon, "0");
        return super.onCreateOptionsMenu(menu);
    }       
}
Community
  • 1
  • 1
Somnath Pal
  • 137
  • 1
  • 4
  • 10

0 Answers0