Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. How to show popup window directly above button

I need to show popup window directly above button. My button is placed inside scroll view, and popup always shown below button. Here is my code:

  private void showPopup(View view, String text) {
    if (infoPopup == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View popupView = layoutInflater.inflate(R.layout.popup_credit_request_passport, null);
        TextView tvPopupText = popupView.findViewById(R.id.tv_popup_text);
        tvPopupText.setMovementMethod(new ScrollingMovementMethod());
        tvPopupText.setText(text);
        FrameLayout flBackground = popupView.findViewById(R.id.fl_background);
        flBackground.setBackground(new BubbleDrawable(getContext(), R.color.azure, 16, 16, 8));

        infoPopup = new PopupWindow(popupView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        infoPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        infoPopup.setOutsideTouchable(true);

        infoPopup.showAtLocation(view, Gravity.TOP, (int) view.getX(), (int) view.getY());

    } else {
        dismissInfoPopup();
    }
}

But it is not work. Please hep me. enter image description here

like image 840
anna Avatar asked Oct 26 '25 15:10

anna


2 Answers

Change Gravity to Top and reset your coordinates according to your requirement.

infoPopup.showAtLocation(view, Gravity.TOP, 0, (int) view.getY());
like image 167
Prachi Singh Avatar answered Oct 28 '25 03:10

Prachi Singh


When you have to show the popup, find out the location of the button in the screen, then use the showAtLocation() method as Prachi Said using Gravity.TOP and the x,y coordinates of the button on screen...

like image 38
kaustav07 Avatar answered Oct 28 '25 04:10

kaustav07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!