7

I am trying to get a popup to behave! =)

The problem is that the popup "fills the width" of the entire screen, even though the layout clearly says that it should "wrap_content". It doesnt matter if I use Dialog or PopupWindow.

First, the XML layout of the popup, 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 I am using:

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 have 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 it always, no matter what code I use, looks like this:

enter image description here

As you can see, its always filling the width of the screen.

Question: Can anyone tell me why?

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

I changed the Button to "wrap_content" (according to Boogers suggestion), and then it looks like this:

enter image description here

This is very strange behaviour, not what I expected.

If I change it to "match_parent", its back to "full width", ie it extends all the way from left to right.

Ted
  • 19,727
  • 35
  • 96
  • 154
  • that's because of your TextView that is in match_parent – njzk2 Jan 24 '13 at 14:31
  • What do you mean? Cause I tried changing that too, doesnt matter - still full width... – Ted Jan 24 '13 at 23:22
  • match_parent means take all space available. wrap_content means take as little as possible. a match_parent in a wrap_content means the parent gets as wide as possible to accomodate the child – njzk2 Jan 25 '13 at 08:32
  • Well, as I said... nothing changes if I change that value. And "match_parent" means "takes space available2, but that shouldnt be the full width, because the top level layout is "wrap_content", so why is it expanding? – Ted Jan 25 '13 at 11:41
  • no reason why wrap_content around match_parent would shrink the child view. makes more sense that it expands the parent view – njzk2 Jan 25 '13 at 11:43

1 Answers1

0

This is your problem:

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

Change that from "fill_parent" to "wrap_content" or "match_parent"

Booger
  • 18,579
  • 7
  • 55
  • 72
  • How is that the problem? It says "fill_parent", and its parent is: 1) the RelativeLayout and 2) the top LinearLayout. So when it fills it parent, it fills the RelativeLayout, but that is not "fill_width"... What I am trying to say is that there is nothing here that "expands" the width... Lets say I want the button to fill the width, whatever it may be. But I have already limited that by the other layouts, as I see it? – Ted Jan 24 '13 at 22:48
  • See my Edit 1 above. Neither or your suggestions seems to be what I am looking for. – Ted Jan 24 '13 at 23:18
  • My suggestion did in fact work for you (and is the correct answer). It is wrapping content as expected. I answered your original question (you now have a different problem - but your dialog is not full width, which is what you were asking). – Booger Jan 24 '13 at 23:25
  • 1
    Ok, thats true. But the behavious is still completely illogical. – Ted Jan 25 '13 at 11:43