4

I'm working on a dialog box in android which I intend to make very flexible for different phone-resolutions. Therefore I'm using the layout_weight attribute when building the xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/newGame_linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/newGame_InvitedPlayers"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@color/ListView_Background"
            android:cacheColorHint="#00000000" >
        </ListView>

        <ListView
            android:id="@+id/newGame_Plugins"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@color/ListView_Background"
            android:cacheColorHint="#00000000" >
        </ListView>
    </LinearLayout>

    <Button
        android:id="@+id/newGame_sendInvite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Invite Players" />

    <TextView
        android:id="@+id/newGame_textViewOnlinePlayers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Online Players:" />

    <ListView
        android:id="@+id/newGame_onlinePlayers"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:background="@color/ListView_Background"
        android:cacheColorHint="#00000000" >
    </ListView>

</LinearLayout>

Here is how it's supposed to look:

desired outcome

And indeed it looks like I want it to.

But when I click the items in the bottom Listview, which removes them from the bottom ListView and adds them to the top left ListView, the bottom Listview decreases in size until I remove the very last item and it disappears completely and looks like this:

current outcome

I want it to not change in size at all. Even when it's empty.

Even though dp is a very flexible size-unit I don't want to use it in this case, the two top ListViews should take 3/5 of the screen and the bottom one gets the remaining 2/5, and the Button and the TextView should take as little space possible in between.

I've tried change the last Listviews height to match_parent, it doesn't change anything. The listview still disappears when I click the items.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
Tejpbit
  • 450
  • 1
  • 4
  • 13
  • 1
    I'm reasonably certain this behaviour is because of the way the dialog attempts to minimize its required size, and hence its content. Basically the height is being wrapped, which you can easily tell by visually comparing the desired and actual results. To override this standard behaviour, you could try manipulating the `LayoutParams` of the `Window` the dialog is attached to. [See here for an example](http://stackoverflow.com/a/6631310/1029225). Alternatively you could probably set up a `Fragment` or `Activity` with the appearance of a dialog. – MH. Nov 11 '12 at 19:48
  • I've tried it now. Oddly enough it doesn't stay at the same size. It reacts in the same way with only one difference. I get a grapical problem at the bottom when the dialog gets smaller. The buttons i got there "Start Game", and "Cancel" gets drawn out like so: http://junk.zomis.net/stackoverflow/layoutparams.png – Tejpbit Nov 11 '12 at 20:10
  • I tried with setting it up with an activity and it works like i want it. Thank you. If you would write it as an answer i could accept it as best answer for other people in the future to see. – Tejpbit Nov 16 '12 at 20:54

1 Answers1

0

Fixe the height for the last ListView and put android:layout_weight="1" for the left and right ListViews parent's layout

HocineHamdi
  • 191
  • 6