I am developing an app where in one of the activities I have a listview. It is a dynamic listview that is populated with its own arrayadapter, so its extension is unknown. I would like that when it exceeds the size of the screen it reaches the bottom and scrolls down.
To achieve this, I have to give the list a fixed size, specifically 471, so that it occupies a large part of the screen and I can scroll up and down. If I give it the match_parent property it stays in a single row, and a single element stays on top, I can scroll but all the elements stay there, up and small.
Is there a way to adjust the list for all screens and make it complete on the screen?
I leave you the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.oftecnica2.agendajose.MainActivity"
android:orientation="vertical"
android:gravity="center"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:id="@+id/lista"
android:divider="@drawable/separador"
android:layout_height="475dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Here the own layout that I use to fill the listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="53dp"
android:layout_height="75dp"
android:id="@+id/imageView"
android:src="@drawable/iconoreunion" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:paddingBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nº"
android:id="@+id/textView"
android:paddingRight="10dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtnumero"
android:paddingRight="30dp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:paddingBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Act1Fecha"
android:id="@+id/textView3"
android:paddingRight="10dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtfecha"
android:paddingRight="10dp"
android:layout_weight="0.25"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Act1Nombre"
android:id="@+id/textView5"
android:paddingRight="10dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtnombre"
android:paddingRight="30dp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The first thing is that having a
listview
inside ascrollview
is a very bad idea, if you need to have a listview with top or bottom views you must add the views above or below it with theaddHeaderView()
and methodsaddFooterView()
.What is surely happening to you is that the scrollView is taking the size of the listview so that the scrollview has the size of the entire screen add:
All the best
You can do this..
So that you don't get complicated... in this simple way you would have your screen only to show the ListView, well if the requirement asks for it.
Have you tried removing the Action Bar?
Firstly in your main container
LinearLayout
, you have defined:Which is correct, but you have padding defined which is going to generate space:
Remove the padding.
Another important point is that you don't need a
Scrollview
to contain the ListView since it has its own Scroll, remove theScrollView
and itsLinearLayout
.This is an example: