Android - Popup window not showing in fragment
Asked Answered
C

6

7

I'm creating a popupWindow but it is not showing on my Fragment when I call it.
Here is my code for the popupWindow:

LayoutInflater layoutInflater = 
            (LayoutInflater)getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
    btnDismiss.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }});

    popupWindow.showAsDropDown(getView());
    //popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
    //popupWindow.showAsDropDown(getCurrentFocus());
    popupView.setOnTouchListener(new OnTouchListener() {
        int orgX, orgY;
        int offsetX, offsetY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                orgX = (int) event.getX();
                orgY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                offsetX = (int)event.getRawX() - orgX;
                offsetY = (int)event.getRawY() - orgY;
                popupWindow.update(offsetX, offsetY, -1, -1, true);
                break;
            }
            return true;
        }});
Craquelure answered 28/1, 2015 at 11:29 Comment(2)
Call a show method on the popupwindow object.Germinative
well its inside the public void popups(), i call that popups on the fragment still not appearing sirCraquelure
Y
9

The following code may be work your specification. Call this method from inside onClick(View v) of OnClickListener assigned to the View:

public void showPopup(View anchorView) {

    View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

    PopupWindow popupWindow = new PopupWindow(popupView, 
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // Example: If you have a TextView inside `popup_layout.xml`    
    TextView tv = (TextView) popupView.findViewById(R.id.tv);

    tv.setText(....);

    // Initialize more widgets from `popup_layout.xml`
    ....
    ....

    // If the PopupWindow should be focusable
    popupWindow.setFocusable(true);

    // If you need the PopupWindow to dismiss when when touched outside 
    popupWindow.setBackgroundDrawable(new ColorDrawable());

    int location[] = new int[2];

    // Get the View's(the one that was clicked in the Fragment) location
    anchorView.getLocationOnScreen(location);

    // Using location, the PopupWindow will be displayed right under anchorView
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                                     location[0], location[1] + anchorView.getHeight());

}

here anchorView is the v from onClick(View v).

Yirinec answered 28/1, 2015 at 11:39 Comment(0)
I
3

Use following code to show Pop Up window.It will work for you.

View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(popupView , Gravity.CENTER, 0, 0);
Illlooking answered 28/1, 2015 at 11:39 Comment(0)
R
2

Here is the sample code to show and hide a popup window.

    TextView popupWindowTextView = new TextView(getActivity()); 
    Button popupWindowButton = new Button(getActivity()); 
    LinearLayout layout = new LinearLayout(getActivity()); 
    popupWindowButton.setText("OK"); 
    popupWindowTextView.setText("Popup Window. Click on OK to dismiss."); 
    popupWindowTextView.setPadding(0, 0, 0, 20); 
    layout.setOrientation(1); 
    layout.addView(popupWindowTextView); 
    layout.addView(popupWindowButton);

    int popupWindowWidth = 200;
    int popupWindowHeight = 150;
    final PopupWindow popupWindow = new PopupWindow(context);
    popupWindow.setContentView(layout);
    popupWindow.setWidth(popupWidth);
    popupWindow.setHeight(popupHeight);
    popupWindow.setFocusable(true);
    popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY,    popupWindowWidth, popupWindowHeight);
    popupWindowButton.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
           popupWindow.dismiss();
         }
    });
Roodepoortmaraisburg answered 12/7, 2016 at 11:53 Comment(0)
B
1
final PopupWindow popupWindow = new PopupWindow(
        popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Check the LayoutParams you are using is matching with the parent of the view. e.g. use if the parent is a LinearLayout, use LinearLayout.LayoutParams.WRAP_CONTENT

Baucis answered 28/1, 2015 at 11:55 Comment(1)
That's not a thing. The other LayoutParams are all inheriting WRAP_CONTENT and MATCH_PARENT from ViewGroup.LayoutParams; they don't have their own values.Defenestration
L
0

The root of R.layout.popup should have

android:layout_width="wrap_content"
android:layout_height="wrap_content"
Lashondra answered 4/12, 2018 at 11:2 Comment(0)
G
0

Just use the update like below immediately after the showAsDropDown :

pop.showAsDropDown(anchor);
pop.update(0,0, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Gwyngwyneth answered 2/10, 2019 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.