Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Custom view for a menu item

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.?

like image 836
Brad Avatar asked Jun 27 '13 00:06

Brad


People also ask

Can you a create custom view how android?

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.

How do I pass custom layout to popupMenu?

OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.

What is custom views in android?

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.


1 Answers

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.

like image 121
Brad Avatar answered Oct 05 '22 23:10

Brad