Reputation: 4721
I have 2 Listviews in 2 different LinearLayouts. The 2 linearlayouts are part of a more complex UI and the height of the 2 LinearLayouts is a weight value. The 2 Listviews inside just fill the parent (LinearLayout). All works fine until I begin to fill the ListViews with items. As I fill the first listView with lets say 10 items, the first listView begins to eat place and get place from the second listview so at the end the second listview occupies less space than the first. It's not a desired behavior. I need the ListView to be fixed.
Here is the code. Will omit not needed code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="9"
android:orientation="vertical" >
...
<LinearLayout
android:id="@+id/secondPart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:weightSum="9.1"
android:layout_weight= "9">
<LinearLayout
android:id="@+id/Screen1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="4.5"
android:orientation="vertical" >
<ListView
android:id = "@+id/lvMain1"
android:orientation="vertical"
android:layout_height = "match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
<LinearLayout
android:id = "@+id/timerContainer2"
android:orientation="horizontal"
android:layout_weight="0.1"
android:layout_height = "0dp"
android:layout_width="match_parent">
....
</LinearLayout>
<LinearLayout
android:id = "@+id/Screen2"
android:orientation="vertical"
android:layout_weight="4.5"
android:layout_height ="0dp"
android:layout_width="match_parent">
<ListView
android:id = "@+id/lvMain2"
android:orientation="vertical"
android:layout_height = "match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
the weightsum for the whole UI is defined and is 9.1
Upvotes: 0
Views: 1269
Reputation: 4292
As per my understanding you want something like this:
XML:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Seperator" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</LinearLayout>
Java Code:
public class MYList extends Activity {
ListView listTOP,listBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
ArrayList<String> data = new ArrayList<String>();
for(int i=0;i<11;i++)
{
data.add("Item "+(i+1));
}
listTOP = (ListView)findViewById(R.id.listView1);
listBottom = (ListView)findViewById(R.id.listView2);
listTOP.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,data));
listBottom.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,data));
}
}
Upvotes: 0
Reputation: 4721
Sorry for time consuming. It was my fault.
In code side I was setting the weight of the Screen1 and Screen2
and somehow I changed the width and height by code:
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params2 =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
I wanted simply to change the weight:
params.weight = 4.5f;
But didn't put the right width and height in the constructor.
Upvotes: 0
Reputation: 17401
change this:
<LinearLayout
android:id="@+id/Screen1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="4.5"
android:orientation="vertical" >
<ListView
android:id = "@+id/lvMain1"
android:orientation="vertical"
android:layout_height = "match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
To:
<ListView
android:id = "@+id/lvMain1"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="4.5"
android:layout_width="match_parent"/>
I am not sure this will solve your exact problem but linear layout is not needed here.
Upvotes: 1