I need to have dynamic Menu Item, a circle of user defined color, like this:
touching this menu item will open a color picker.
Now, I have sample ColorPickerIcon which extends View
public class ColorPickerIcon extends View { private Paint mPaint; private int mColor; private final int mRadius = 20; public ColorPickerIcon(Context context) { super(context); mColor = Color.BLACK; mPaint = createPaint(); } public ColorPickerIcon(Context context, AttributeSet attrs) { super(context, attrs); mColor = Color.BLACK; mPaint = createPaint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(0, 0, mRadius, mPaint); } public void setPaintColor(int color) { mColor = color; } private Paint createPaint() { Paint temp = new Paint(); temp.setAntiAlias(true); temp.setStyle(Paint.Style.STROKE); temp.setStrokeJoin(Paint.Join.ROUND); temp.setStrokeWidth(6f); temp.setColor(mColor); return temp; } }
and menu.xml
<item android:id="@+id/menu_pick_color" android:title="@string/pick_color" yourapp:showAsAction="always" yourapp:actionViewClass="com.example.widgets.ColorPickerIcon"/> <item android:id="@+id/menu_clear" android:icon="@null" android:title="@string/clear" yourapp:showAsAction="always"/> <item android:id="@+id/menu_save" android:icon="@null" android:title="@string/save" yourapp:showAsAction="always"/>
But it doesn't work this way, neither can I instantiate the class nor it's rendered. Is there a way to use custom class and custom dynamic view as Menu Item?
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.
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 inflate popupMenu from a button onClick , use the following code. You can either add some styles to the popupMenu and achieve your UI or create popup Window. My conclusion is; if we define a style with parent="@android:style/Widget. PopupMenu" as parent, we override behaviour of our PopupMenu.
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; }
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