Get View from MenuItem in PopupMenu

1.9k views Asked by At

I need to get the coordinates of the MenuItem that's been clicked on in a PopupMenu. The only way I know of to do this is to get a View from the MenuItem, but I don't know how to do that. I tried this but it returns null:

View view = ((SelectPlanningActivity) context).getWindow().getDecorView().findViewById(R.id.action_edit_planning);

The full code of my PopupMenu (it is in an adapter for a RecyclerView):

PopupMenu popup = new PopupMenu(context, v, Gravity.RIGHT);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_planning_card, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    View view = ((SelectPlanningActivity) context).getWindow().getDecorView().findViewById(R.id.action_edit_planning);
                    int[] coordinates = Utils.getCenterCoordinates(context, view); // I need to use the View here or use a different way to get its position on the screen
                    Intent i = new Intent(context, EditPlanningActivity.class);
                    i.putExtra("planning", planning);
                    i.putExtra("startX", coordinates[0]);
                    i.putExtra("startY", coordinates[1]);
                    ((SelectPlanningActivity) context).startActivityForResult(i, Utils.ACTIVITY_RESULT_EDIT_PLANNING);
                    return true;
                }
            });
            popup.show();

Can anyone help me?

1

There are 1 answers

0
DennisMuchiri On

try:

MenuItem menuitem =popup.getMenu().findItem(R.id.yourmenuitemid);                                                              
menuitem.getActionView().setBackgroundColor(getResources().getColor(R.color.app_menuitem orange));