0

I'd like the top half of my screen to be a graph, and the bottom part to be a listview. Both parts should occupy half of the screen.

With the setup I have now it only works if there are like 10 or more items in the listview. If there are less, the listview will take up less than half the screen. If there's one item in the list, it takes up even less the the height of one list item.

Based on the solution of ANDROID : split the screen in 2 equals parts with 2 listviews this is what I've got now:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
        >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.5"
            android:id="@+id/graph_container"
            android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.5"
            android:orientation="vertical">

        <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false"/>
    </LinearLayout>

 </LinearLayout>

I add the graph dynamically:

((LinearLayout) findViewById(R.id.graph_container))
            .addView(chart, 0, 
                new  LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 
                    ViewGroup.LayoutParams.MATCH_PARENT));

Does anyone know how I should do this?

UPDATE

So, the essential part was changing the layout_height to math_parent. Removing the parent linear layout of the listview was optional, but definitely cleaner.

Community
  • 1
  • 1
Divisible by Zero
  • 2,616
  • 28
  • 31

4 Answers4

3

Remove container that listView stays in, change the weight to 1 and set height of the main container to match_parent. Here is an example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/graph_container"
        android:orientation="vertical">

</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true"
    android:drawSelectorOnTop="false" >
</ListView>

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
1

You just have to change your first LinearLayout height to match_parent

Leaudro
  • 1,021
  • 8
  • 7
  • Thanks for your answer Leaudro! You're answer is correct, but I picked Marcins answer because his answer also included an optimization for my layout. – Divisible by Zero Apr 16 '13 at 05:38
1

Try to use weight values as digits 1:1 fox example.

Artemis
  • 4,821
  • 3
  • 21
  • 24
1

Setting the android:weightSum="1" and android:layout_height="match_parent" for the parent LinearLayout should work.

Ashwini Bhangi
  • 291
  • 1
  • 6
  • Setting android:layout_height="match_parent" for the parent LinearLayout was essential, but setting the weightSum is not necessary. from the [docs](http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum) "If unspecified, the sum is computed by adding the layout_weight of all of the children." – Divisible by Zero Apr 16 '13 at 05:41