Reputation: 860
I have implemented a listview in my Android app (API 8). However, the list consists of only three elements and hence, more than half of the screen is empty. I want to expand the listview to cover the entire screen of the phone. What are some ways to do the same?
Here is my layout XML file:
<?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"
android:background="@color/white"
android:shape="rectangle"
android:paddingTop="5dp"
android:paddingBottom="0dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<ImageView
android:layout_weight="40"
android:id="@+id/test_image"
android:layout_width="158dp"
android:layout_height="52dp"
android:contentDescription="@string/footer"
android:src="@drawable/logo1" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="60"
android:prompt="@string/city_prompt"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@android:id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:drawSelectorOnTop="true"
android:layout_gravity="center"
android:dividerHeight="2dp"
/>
</LinearLayout>
</LinearLayout>
Please note that I want the list items to cover the entire screen, with just three items in place.
Upvotes: 0
Views: 14670
Reputation: 67286
I just copied your XML, pasted and tried, and it works fine. One more thing to tell is that you should not use layout_height="wrap_content"
. Instead you can give it as android:layout_height="0dip"
as you have already used android:layout_weight="1"
which will make ListView fill the remaining area of your LinearLayout.
Upvotes: 2
Reputation: 29199
ListView won't fill all the space automatically. If you want to achieve this, because here, ListView is taking the full space, but not its items. If you still want to make bigger rows, in case of lesser items, then provide the height of list items, in the getView method using the getCount() method.
For example, you want to display maximum six items at a time on screen in the list. Then Implement ListView like this:
Get width and height of screen, using the following code into the onCreate method:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Now in getView, do the following:
int itemHeight = 0;
if (getCount() >= 6)
itemHeight = height/6;
else
itemHeight = height/getCount();
LinearLayout llView=new LinearLayout(YourActivityName.this);
//Or inflate through XML
llView.setLayoutParams(new LayoutParams(LayoutParams.Fill_Parent, itemHeight));
//Do other functionality
Upvotes: 2
Reputation: 1179
First change:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white"
android:shape="rectangle"
android:paddingTop="5dp"
android:paddingBottom="0dp"
>
Tthen second, change:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:drawSelectorOnTop="true"
android:layout_gravity="center"
android:dividerHeight="2dp"
/>
Upvotes: 1