0

I am dividing my screen into three parts using weights. In one of them i have a listview and in the other two I have imageviews. But my listview occupies the entire screen. Can someone logically explain what I am doing wrong here?

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

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

And the listview is populated as

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"first","second"});
    listview = (ListView)this.findViewById(R.id.listView1);
    listview.setAdapter(arrayAdapter);
    }
Yagna
  • 423
  • 1
  • 5
  • 15
  • set the width of your listview to 0dp, and all of them that contain a weight for that matter – zgc7009 Oct 03 '14 at 19:53
  • As soon as i set width of my listview to 0dp. The list view is invisible. The screen is now properly divided into portions but the list is not visible, – Yagna Oct 03 '14 at 19:57
  • Sorry, meant your listview container, didnt read it well... but my second part of the statement was right :P Just wherever you set a weight make sure your width is 0. The reason for that is because if you set an actual size other than 0, it measures that size, and then remeasures to handle the weight. This is inefficient, it is best to just set it to 0 and let weight do its thing – zgc7009 Oct 03 '14 at 19:59
  • Yes . It worked by setting all the container widths to 0dp. Like Chitrang mentioned below. Thanks. Can you kindly explain the reason why 0dp has to be set for the containers? – Yagna Oct 03 '14 at 20:02
  • Take a look at the comment, if you set a weight then what it will do is it will measure for your layout_width attribute, and then remeasure to handle your weight. Since it is already measuring for the weight, just set your width to 0 or you will be remeasuring all of your weighted views. – zgc7009 Oct 03 '14 at 20:05

1 Answers1

1

Try this,

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

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

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

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

Chitrang
  • 5,097
  • 1
  • 35
  • 58
  • Thanks it worked. Can you explain why container width has to be set as 0dp? – Yagna Oct 03 '14 at 20:05
  • @yagnasri take a look at my comment in your question – zgc7009 Oct 03 '14 at 20:05
  • Can you follow this, http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean and http://developer.android.com/guide/topics/ui/layout/linear.html – Chitrang Oct 03 '14 at 20:08