Using WRAP_CONTENT, popupwindow gets hole screen width

1.1k Views Asked by At

I am trying to create a PopupWindow with custom layout. Here is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="48dp" >

    <ImageView
        android:id="@+id/switch_iamge"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4dp"
        android:src="@drawable/switch_icon" />

    <TextView
        android:id="@+id/old_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/switch_iamge" />

</RelativeLayout>

And here I am creating PopupWindow:

PopupWindow popupWindow = new PopupWindow(view,
        LayoutParams.WRAP_CONTENT, 48);

However it gets hole screen width. I also checked this and this, but they didn't help. What should I do?

Edit: It seems that the problem is in ImageView. Also using wrap_content in image don't work. So I removed ImageView and used drawableRight property in TextView and it works.

1

There are 1 best solutions below

1
On

You have to create the popup window programmatically. You can't change it in the XML file. I faced the same problem. Put match_parent in your xml instead of wrap_content. Do something like this:

PopupWindow popupWindow = new PopupWindow(view,120,220);

Hopefully, it helped :)