Home » Android » layout – Popup window size in android

layout – Popup window size in android

Posted by: admin May 14, 2020 Leave a comment


I’m creating a popup window in a listactivity in the event onListItemClick.

LayoutInflater inflater = (LayoutInflater)
View pop = inflater.inflate(R.layout.popupcontact, null, false);
ImageView atnot = (ImageView)pop.findViewById(R.id.aNot);
height = pop.getMeasuredHeight();
width = pop.getMeasuredWidth();
Log.e("pw","height: "+String.valueOf(height)+", width: "+String.valueOf(width));
atnot.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

 pw = new PopupWindow(pop, width, height, true);

 // The code below assumes that the root container has an id called 'main'
 //pw.showAtLocation(v, Gravity.CENTER, 0, 0); 
 pw.showAsDropDown(v, 10, 5);

Now, the height and width variables were supposed to be height and width of the layout used for the popup window (popupcontact). But they return 0. I guess that is because the layout isn’t rendered yet. Does anyone have a clue, how can I control the size of the popup window without needing to use absolute pixel numbers?

How to&Answers:

I don’t think your code will work because you are trying to get the width and height of the view that is a child of your popup window.

Since you are anchoring the popupwindow to the “v” view, maybe set the width and height according to the anchor view (or at a ratio based on them)?


I had a similar issue. I solved it by calling:

pop.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

before getting the size of the layout with getMeasuredHeight() and getMeasuredWidth()


If you want to create a Popup window take a look at the Creating Dialogs.
If you want to use a complete own View in your dialog have a look at this question make sure to read the comments on the accepted answer. I think there is no need to use PopupWindow yourself to create this view and the dialog stuff will get you to a result much easier.

If you want to stick with the PopupWindow and you don’t want to change the width and height of your window just use another constructor for PopupWindow there are some available that don’t need height and width. They don’t take a boolean for the ability to focus the window but it seems you can change this later on via setFocusable().