I want to be able to specify a custom view for a MenuItem.
I want to display an indicator on a shopping cart menu item in the action bar displaying how many items are in the cart currently.
Is the only way that I can do this is through adding a custom view instead of utilising the MenuItem lifecycle etc.?
Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.
OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.
Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.
To use a custom view on a MenuItem call setActionView(View view).
You can supply either an already inflated view or a layout resource ID.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With