A popup window (using PopupWindow or Dialog) fills the width of the screen for no apparent reason

I'm trying to make a popup behave! =)

The problem is that the popup window “fills the width” of the entire screen, although the layout explicitly says that it should “wrap_content”. It doesn’t matter if I use Dialog or PopupWindow.

First, an XML popup window layout, popup_message.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="20dp" android:background="@android:color/transparent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:layout_gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="@android:color/transparent" android:gravity="center_vertical|center_horizontal" android:orientation="horizontal" android:paddingLeft="10dp" android:text="@string/new_message" android:textColor="#000000" android:textAllCaps="true" > </TextView> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:background="#ffffff" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/popup_message_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:textCursorDrawable="@null" > </TextView> <Button android:id="@+id/jobview_pickup_start_submit" android:layout_width="fill_parent" android:layout_height="35dp" android:layout_below="@+id/popup_message_textView" android:layout_gravity="center_vertical|center_horizontal" android:layout_marginBottom="15dp" android:layout_marginTop="15dp" android:gravity="center_horizontal" android:text="Confirm" android:textColor="#000000" /> </RelativeLayout> </LinearLayout> 

And the code that I use:

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View input = inflater.inflate(R.layout.popup_message, null); PopupWindow pw = new PopupWindow(input, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); View v = findViewById(R.id.main_deviceInfoBar); pw.showAtLocation(v, Gravity.CENTER, 0, 0); 

I also tried this:

Dialog dialogManualStart = new dialog (MainActivity.this); . DialogManualStart.getWindow () setBackgroundDrawableResource (R.color.transparent); dialogManualStart.requestWindowFeature (MainActivity.this.getWindow () FEATURE_NO_TITLE.); dialogManualStart.setContentView (input); dialogManualStart.show ();

And always, no matter what code I use, it looks like this:

enter image description here

As you can see, it always fills the width of the screen.

Question: Can someone tell me why?

======== EDIT 1 ==============

I changed Button to "wrap_content" (as suggested by Boogers), and then it looks like this:

enter image description here

This is a very strange behavior, not what I expected.

If I change it to match_parent, its back to full width, i.e. it spreads completely from left to right.

+6
source share
1 answer

It's your problem:

 <Button android:id="@+id/jobview_pickup_start_submit" android:layout_width="fill_parent" ... 

Change this value from "fill_parent" to "wrap_content" or "match_parent"

0
source

All Articles