Search code examples
androidlistviewandroid-popupwindow

PopWindow always show at last in ListView...!



I working on android PopupWindow. In this application I create 1 custom ListView. Now I want to show PopupWindow when user click on TextView of custom ListView.
My problem is :
PopupWindow always show at last TextView even if I click on first or other TextView .
How can I resolve this.??

TextView of ListView.

holder.end.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                holder.end.getLocationOnScreen(location);
                p = new Point();
                p.x = location[0];
                p.y = location[1];
                if (p != null){
                    showPopup(context,p);
                    holder.popupText.setText(holder.end.getText().toString());
                }
            }
        });

My popupWindow function.

private void showPopup(final Activity context, Point p) {

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout)context.findViewById(R.id.layoutPopup);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        holder.popupText = (TextView) layout.findViewById(R.id.showPopUp);
        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popup.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popup.setFocusable(true);

        int OFFSET_X = 30;
        int OFFSET_Y = 30;

        popup.setBackgroundDrawable(new BitmapDrawable());
        popup.showAtLocation(layout.findViewById(R.id.showPopUp), Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

    }

Solution

  • Tray changing your click listener with this code. It should work.

    holder.end.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationOnScreen(location);
                p = new Point();
                p.x = location[0];
                p.y = location[1];
                if (p != null){
                    showPopup(context,p);
                    holder.popupText.setText(holder.end.getText().toString());
                }
            }
        });