Search code examples
androidpopupmenumousehover

how can I create this type of menu and call on Hover action in android?


enter image description here
I want to create custom menu like this. Can anyone help me???

Here is my java side implementation. ivPricePopupMenu is ImageView object and productlist_popup_menu is layout which I want to display on onMouseHover action. Suggest some solutions.

ivPricePopupMenu.setOnHoverListener(new View.OnHoverListener() {
            @Override
            public boolean onHover(View v, MotionEvent event) {
                Log.d("hover", "Bring yor cursor over the button");
                if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
                {
                    //instantiate the popup.xml layout file
                    LayoutInflater layoutInflater = (LayoutInflater) ProductListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View customView = layoutInflater.inflate(R.layout.productlist_popup_menu,null);

                    //instantiate popup window
                   PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                    //display the popup window
                    popupWindow.showAtLocation(linearLayout, Gravity.CENTER,0, 0);
                }
                return false;
            }
        });

Solution

  • Hover menu or tooltip kind of view can be implemented using PopupWindow widget.

    PopupWindow(context)

    Create the required XML layout file for the PopupWindow. Inflate the layout using LayoutInflater and set the required data to it.

    Calculate the x and y coordinates based on the anchor view's location on screen (i.e., the info button view in the image from the question) to position the pop-up window.

    Show the pop-up window using showAtLocation method.

    popUpWindow.showAtLocation(anchorView, gravity, x, y)
    

    gravity can be set to Gravity.NO_GRAVITY by calculating x and y coordinates correctly.