2

I am displaying JSON data inside RecyclerView , I added long press action on RecyclerView item , which display PopupWindow. I want to display that PopupWindow on Top of each Recyclerview item when i long click on it.

but PopupWindow only display on TOP of parent screen not on top of Recyclerview item.

i want that PopupWindow should display at TOP of each RecyclerView item layout.

here is my code for Displaying PopupWindow

private void PopUpWindowOption(LinearLayout mainLayout)
    {

        View customView = LayoutInflater.from(context).inflate(R.layout.popup,null);

        Button closePopupBtn = (Button) customView.findViewById(R.id.closePopupBtn);

        final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);



        closePopupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show();
            }
        });

        popupWindow.showAtLocation(mainLayout, Gravity.TOP, 0, 0);

    }

Output

enter image description here

How can i achieve this help me. Thank you

1 Answers1

3

what you are currently using is :

popupWindow.showAtLocation(mainLayout, Gravity.TOP, 0, 0);

What you want to use is :

popupWindow.showAsDropDown(anchor, offsetX, offsetY, gravity);

The above code shows the popup relative to the specified anchor view. you can use the view of the item at the time of OnClick or OnLongClick Event. This will show as a dropdown relative to the item. If you want to show it above the itemView, You can change it accordingly by changing the value of offsetY. like :

popupWindow.showAsDropDown(anchor, 0, -anchor.getHeight() + popupView.getHeight());
Nikunj Peerbits
  • 785
  • 4
  • 12
  • popupWindow.showAsDropDown(anchor, 0, -anchor.getHeight() + popupView.getHeight()); code is work for me , now is it display on top of my Recyclerview item layout.... thanks – Pritesh Vishwakarma May 09 '18 at 06:23