1

I know this question has been asked a lot of time, and I've read through a lot of them but I don't seem to find an answer. I'm sorry if there is an answer out there that I've not seen.

My problem is that I want to make a screen that can re-size to any screen size. In order to accomplish that, I don't want to set any kind of static width or height in the layouts.

The problem is that I want to have nested weights. The reason for that is that I have a ListView inside of an LinearLayout in between a TextView and another LinearLayout.

The LinearLayout that contains the ListView is composed of the ListView itself and another LinearLayout.

I want the ListView and the LinearLayout(with the two buttons with arrows) to be weighted. I also want the whole screen to be weighted in the next fashion:

  • The four buttons on the bottom of the screen must be weighted
  • The TextView, the intermediate LinearLayout (With the ListView) and the Bottom LinearLayout (With the four buttons) must be weighted.

I made a quick representation of the screen so you guys could understand this better.

This is the screen design

The solution I thought was to change the most external layout to a RelativeLayout. The problem is that if i do that, The ListView will just push the bottom LinearLayout out of the screen.

Is there a way to weight RealtiveLayout components or at least an implementation with LinearLayout that is not considered "Bad for perfomance"?

I'll put the Layout code next:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/multiPlayerHubLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/multiplayerHubHeaderTextView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="@string/game_inventory_header" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:layout_weight="10" >

    <ListView
        android:id="@+id/game_inventory_listView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10" >
    </ListView>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/game_inventory_upButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/game_all_upButton" />

        <Button
            android:id="@+id/game_inventory_downButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/game_all_downButton" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/game_inventory_scanButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/game_all_scanButton" />

    <Button
        android:id="@+id/game_inventory_supportButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/game_inventory_support" />

    <Button
        android:id="@+id/game_inventory_attackButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/game_inventory_offensive" />

    <Button
        android:id="@+id/game_inventory_healingButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/game_inventory_healer" />
</LinearLayout>

Thanks in advance!

Bruno Follon
  • 422
  • 3
  • 13

2 Answers2

1

If you use the RelativeLayout, you can first declare the LinearLayout with the Buttons and then the one with the ListView, that will keep the ListView from pushing the Buttons off the screen, as it will only take up the remaining space after the Buttons were placed. The layout_weight will not work within a RelativeLayout.

small example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/multiPlayerHubLinearLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin">

  <TextView
    android:id="@+id/multiplayerHubHeaderTextView"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentTop="true"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="@string/game_inventory_header" />

  <LinearLayout  android:id="@+id/buttonLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">
      <!-- Buttons here -->
  </LinearLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:layout_above="@id/buttonLayout"
    android:layout_below="@id/multiplayerHubHeaderTextView"
    android:orientation="horizontal">

    <!-- ListView etc. here -->
  </LinearLayout>


</RelativeLayout>

The drawback of this approach is that you can't use the weights, but are they important for the TextView and the button bar?

tknell
  • 9,007
  • 3
  • 24
  • 28
  • This one worked perfectly! Thank you so much. The key was the android:layout_alignParentBottom="true". I didn't put that one when trying before asking. Thanks a lot. – Bruno Follon Jun 20 '14 at 10:54
1
Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/multiPlayerHubLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/multiplayerHubHeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/game_inventory_header" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/game_inventory_listView"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">
            </ListView>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/game_inventory_upButton"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="@string/game_all_upButton" />

                <Button
                    android:id="@+id/game_inventory_downButton"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="@string/game_all_downButton" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/game_inventory_scanButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/game_all_scanButton" />

        <Button
            android:id="@+id/game_inventory_supportButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/game_inventory_support" />

        <Button
            android:id="@+id/game_inventory_attackButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/game_inventory_offensive" />

        <Button
            android:id="@+id/game_inventory_healingButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/game_inventory_healer" />
    </LinearLayout>
</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Yeah, this solution worked but I still got a warning of "useless layout" from the new LinearLayout. To be honest I'm not sure what made this implementation work, hehe. Thank you so much anyways, the solution from @tknell worked too and I think I'll use that one. Thanks – Bruno Follon Jun 20 '14 at 10:51
  • when you use nested way layout_weight then it's always show warring "Nested weight are bar for performance" – Haresh Chhelana Jun 20 '14 at 10:57