31

I have two listviews, but they don't scroll. How do I correct this?

Here is my layout.xml

 <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/backgrund" >

            <!-- Header  Starts -->

            <LinearLayout
                android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:layout_alignParentTop="true"
                android:background="@layout/header" >
            </LinearLayout>

            <!-- Header Ends -->
            <!-- Footer Start -->

            <TextView
                android:id="@+id/textAD"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/header"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="14dp"
                android:layout_marginRight="26dp"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#FFFFFF" />

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header"
                android:layout_gravity="center_horizontal"
                android:focusable="false"
                android:paddingBottom="5px"
                android:paddingTop="10px"
                android:src="@android:drawable/divider_horizontal_bright" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="#000000"
                android:focusable="false" />

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/header"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textm"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Malzemeler" 
                    android:textSize="20dp"
                    android:textColor="#000000"/>

                <EditText
                    android:id="@+id/editaramalzeme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

                <Button
                    android:id="@+id/btnmalzlist"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusable="false"
                    android:text="Ara" />

                <ListView
                    android:id="@+id/mylist"
                    android:layout_width="match_parent"
                    android:layout_height="420dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF" >
                </ListView>

                <ListView
                    android:id="@+id/listsecili"
                    android:layout_width="wrap_content"
                    android:layout_height="210dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"  >
                </ListView>

                <EditText
                    android:id="@+id/txtNot"
                    android:layout_width="match_parent"
                    android:layout_height="88dp"
                    android:ems="10"
                    android:gravity="top"
                    android:inputType="textMultiLine"
                    android:lines="6"
                    android:singleLine="false" >

                    <requestFocus />
                </EditText>
            </LinearLayout>

            <Button
                android:id="@+id/btnkaydet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/linearLayout1"
                android:text="malzeme ekle" />

            <Button
                android:id="@+id/btntoplugonder"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/textAD"
                android:layout_below="@+id/btnkaydet"
                android:text="toplu gonder" />
        </RelativeLayout>

    </ScrollView>**
Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33
ozi
  • 317
  • 1
  • 3
  • 5

9 Answers9

83

Never put ListView in ScrollView. ListView itself is scrollable.

Pandaiolo
  • 11,165
  • 5
  • 38
  • 70
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
15

By default ListView is scrollable. Do not put ScrollView to the ListView

Prasanna
  • 236
  • 1
  • 6
8

I know this question is 4-5 years old, but still, this might be useful:

Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. That's because the operating system doesn't view it as actually exceeding the screen.

I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. And it was a real mystery. As soon as I added a few more, it started to scroll.

So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". It did, for me.

Robert Ruxandrescu
  • 627
  • 2
  • 10
  • 22
7

Practically its not good to do. But if you want to do like this, just make listview's height fixed to wrap_content.

android:layout_height="wrap_content"
Dhrupal
  • 1,863
  • 1
  • 23
  • 38
3

Listview so have inbuild scrolling capabilities. So you can not use listview inside scrollview. Encapsulate it in any other layout like LinearLayout or RelativeLayout.

Yashdeep Patel
  • 3,090
  • 1
  • 17
  • 21
3

This is my working code. you may try with this.

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listEmployeeDetails"
        android:layout_height="match_parent" 
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_gravity="center"
        android:background="#ffffff">

        <TextView android:id="@+id/tvEmpId"
                      android:layout_height="wrap_content"
                      android:textSize="12sp"
                      android:padding="2dp"
                      android:layout_width="0dp"
                      android:layout_weight="0.3"/>
            <TextView android:id="@+id/tvNameEmp"
                      android:layout_height="wrap_content"
                      android:textSize="12sp"                     
                      android:padding="2dp"
                      android:layout_width="0dp"
                      android:layout_weight="0.5"/>
             <TextView
                    android:layout_height="wrap_content"
                    android:id="@+id/tvStatusEmp"
                    android:textSize="12sp"
                    android:padding="2dp"
                    android:layout_width="0dp"
                    android:layout_weight="0.2"/>               
</LinearLayout> 

details.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listEmployeeDetails"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/page_bg"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/lLayoutGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/page_bg"
        android:orientation="vertical" >

        ................... others components here............................

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alwaysDrawnWithCache="true"
            android:dividerHeight="1dp"
            android:horizontalSpacing="3dp"
            android:scrollingCache="true"
            android:smoothScrollbar="true"
            android:stretchMode="columnWidth"
            android:verticalSpacing="3dp" 
            android:layout_marginBottom="30dp">
        </ListView>
    </LinearLayout>
</RelativeLayout>

Adapter class :

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<EmployeeBean> employeeList; 

    publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
            this.context = context;
            this.employeeList = employeeList;
        }

    public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            EmployeeBeanHolder holder = null;
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);

            holder = new EmployeeBeanHolder();
            holder.employeeBean = employeeList.get(position);
            holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
            holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
            holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);

            row.setTag(holder);
            holder.tvEmpId.setText(holder.employeeBean.getEmpId());
            holder.tvName.setText(holder.employeeBean.getName());
            holder.tvStatus.setText(holder.employeeBean.getStatus());

             if (position % 2 == 0) {
                    row.setBackgroundColor(Color.rgb(213, 229, 241));
                } else {                    
                    row.setBackgroundColor(Color.rgb(255, 255, 255));
                }        

            return row;
        }

   public static class EmployeeBeanHolder {
        EmployeeBean employeeBean;
        TextView tvEmpId;
        TextView tvName;
        TextView tvStatus;
    }

    @Override
    public int getCount() {
            return employeeList.size();
        }

    @Override
    public Object getItem(int position) {
            return null;
        }

    @Override
    public long getItemId(int position) {
            return 0;
    }
}

employee bean class:

public class EmployeeBean {
    private String empId;
    private String name;
    private String status;

    public EmployeeBean(){      
    }

    public EmployeeBean(String empId, String name, String status) {
        this.empId= empId;
        this.name = name;
        this.status = status;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId= empId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status =status;
    }
}

in Activity class:

onCreate method:

public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();

//create emplyee data
for(int i=0;i<=10;i++) {
  EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
  EMPLOYEE_LIST .add(emplyee );
}

ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
Md. Kamruzzaman
  • 1,895
  • 16
  • 26
2

Putting ListView inside a ScrollView is never inspired. But if you want your posted XML-like behavior, there're 3 options to me:

  1. Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).

  2. Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)

  3. If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect.
    To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below:

listView.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
    return (event.getAction() == MotionEvent.ACTION_MOVE);
  }
});
Community
  • 1
  • 1
Touhid
  • 731
  • 1
  • 10
  • 25
0

I found a tricky solution... which works only in a RelativeLayout. We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview
    android:clickable="false" />

<View
    android:layout_width="match_parent"
    android:background="@drawable/gradient_white"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_centerHorizontal="true"
    android:layout_alignTop="@+id/listview" />
Tropp
  • 23
  • 2
0

I had this problem and I found the solution. In my case the listView was bigger than the screen, so the list was not big enough to need scroll, but I coudn´t see it because half list was out of screen.

I solved it adding enough marginBotton to make the listView shorter and it suits to the screen.

<ListView
        android:id="@+id/lvDatos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="300dp"/>
Javierungo
  • 81
  • 9