how to make scrollable ListView but not to fill whole screen?
Asked Answered
H

4

5

I want to make screen with TextView on top (title) a ListView in the middle and buttons on the bottom. How to place ListView that will fill entire space between top TextView and bottom Buttons and be able to scroll its content ?

Now, when my list grows, it pushes bottom button outside the screen.

I have:

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

    <Button android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_width="wrap_content"
    />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:layout_alignParentBottom="true"
    />
    <ListView android:id="@+id/lvLocations"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
    <TextView android:layout_height="0dip"
        android:layout_width="fill_parent"
        android:layout_weight="1" 
    />
    <Button android:text="LayerDrawable"
        android:id="@+id/button5"
        android:textSize="15dp"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:background="@drawable/layer_drawable"
    />

</LinearLayout>

The last TextView is used to make a gap and move Button to a bottom.

Hormuz answered 31/8, 2011 at 17:37 Comment(0)
C
13

Use a LinearLayout. Set the layout_weight="1" to your ListView with layout_height="fill_parent". Remove weight of other elements.

<ListView android:id="@+id/lvLocations"
  android:layout_width="match_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
></ListView>
Cesta answered 31/8, 2011 at 17:41 Comment(0)
C
3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
    <TextView
        android:id="@+id/title"
        android:text="arg0"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" android:textStyle="bold"/>
  </LinearLayout>

  <ListView
      android:id="@+id/list"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:layout_weight="1"/>

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
    <Button
        android:id="@+id/prevButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Previous" 
        android:layout_weight="1"/>
    <Button
        android:id="@+id/nextButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Next" 
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>

This is exactly how I have mine set up, and it works great.

Camphor answered 31/8, 2011 at 17:46 Comment(6)
You should avoid nested Layouts.Effervesce
I try to avoid deeply nested layouts. I don't think that nesting down one level is bad.Camphor
Most of the time you'll be able to use a RelativeLayout where you use a nested one.Effervesce
I know, I was having stupid issues with it and just stuck to what I understood better. RelativeLayouts are definitely handy, I just wasn't used to them when I wrote this.Camphor
I guess nesting layouts is fully acceptable. that's why there is a full View, ViewGroup etc. hierarchy. Thanks for code :) I have to learn more about layout_weight="1"Hormuz
That says that it takes up as much space as all the other widgets with weight = 1.Camphor
D
0

Wrap all your elements in a layout: LinearLeyout or RelativeLayout.

Domel answered 31/8, 2011 at 17:40 Comment(0)
S
0

You can use help of android:layout_below="" and android:layout_above="" to achieve the desired result. Hope this helps.

Scherer answered 4/1, 2016 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.