I need to have dynamic Menu Item, a circle of user defined color, like this:
Okay, so it turned out to be simpler than that.
In the DrawingActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_drawing, menu);
MenuItem colorPicker = menu.findItem(R.id.menu_pick_color);
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
circle.getPaint().setColor(Color.GREEN);
circle.setIntrinsicHeight(120);
circle.setIntrinsicWidth(120);
circle.setBounds(0, 0, 120, 120);
colorPicker.setIcon(circle);
return true;
}
in menu.xml
<item
android:id="@+id/menu_pick_color"
android:title="@string/pick_color"
yourapp:showAsAction="always"/>
That's all.
What you need to do is create a layout file with the view that you want for the item, the when you declare the item on the menu, assign the layout like this:
<item
android:id="@+id/menu_pick_color"
android:title="@string/pick_color"
app:showAsAction="always"
app:actionLayout="@layout/my_custom_item"/>
And that's it!
EDIT:
To access the custom item and modify it's color at runtime you can do this.
In your activity (or fragment) override the onPrepareOptionsMenu
(Assuming you already inflated your menu with 'onCreateOptionsMenu')
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_pick_color);
//Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use
FrameLayout rootView = (FrameLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id);
//And from here you can do whatever you want with your control
return true;
}