2

I m getting problem in making ListView Scrollable. I know that listview is itself scrollable.But i have to put in ..Please tell how can i make listView Scrollable....Thankyou very much

 <?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" >

<RelativeLayout
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="true"



  android:layout_height="597dp"
  android:orientation="vertical" >

   <TextView
    android:id="@+id/TextViewMob"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/textViewCNIC"
    android:layout_below="@+id/textViewCNIC"
    android:layout_marginTop="20dp"
    android:text="Customer Mobile No."
    android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
    android:id="@+id/TextViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/TextViewMob"
    android:layout_below="@+id/editTextMob"
    android:layout_marginTop="20dp"
    android:text="Customer Name"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
    android:id="@+id/TextViewAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/TextViewName"
    android:layout_below="@+id/TextViewName"
    android:layout_marginTop="20dp"
    android:text="Mailing Address"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
    android:id="@+id/EditTextAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextViewAdd"
    android:layout_marginTop="20dp"
    android:layout_toRightOf="@+id/TextViewAdd"
    android:ems="10"
    android:inputType="textMultiLine" />

    <TextView
    android:id="@+id/TextViewKin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextViewAdd"
    android:layout_marginTop="20dp"
    android:layout_toLeftOf="@+id/EditTextAdd"
    android:text="Customer&apos;s Next of Kin"
    android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
    android:id="@+id/EditTextKin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextViewKin"

       android:layout_toRightOf="@+id/TextViewKin"
    android:layout_marginTop="20dp"
     android:ems="10"
       android:inputType="textMultiLine" />

 <EditText
  android:id="@+id/editTextName"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_above="@+id/EditTextAdd"
  android:layout_marginTop="20dp"
  android:layout_toRightOf="@+id/TextViewName"
  android:ems="10" >


   </EditText>

  <TextView
  android:id="@+id/textViewPhoto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@+id/TextViewKin"
  android:layout_marginTop="20dp"
  android:layout_toLeftOf="@+id/EditTextKin"
  android:text="Attach File"
  android:textAppearance="?android:attr/textAppearanceMedium" />

  <Button
  android:id="@+id/buttonCamera"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/textViewPhoto"
  android:layout_alignBottom="@+id/textViewPhoto"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="20dp"
  android:text="From Camera" />

  <Button
  android:id="@+id/buttonAttach"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/buttonCamera"
  android:layout_alignBottom="@+id/buttonCamera"
  android:layout_marginLeft="18dp"
  android:layout_marginTop="20dp"
  android:layout_toRightOf="@+id/buttonCamera"
  android:text="From Sdcard" />



 <TextView
  android:id="@+id/textViewCNIC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:layout_marginTop="65dp"
  android:layout_toLeftOf="@+id/buttonCamera"
  android:text="Customer CNIC"
  android:textAppearance="?android:attr/textAppearanceMedium" />

  <EditText
  android:id="@+id/EditTextCNIC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/textViewCNIC"
  android:layout_alignBottom="@+id/textViewCNIC"
  android:layout_toRightOf="@+id/textViewCNIC"
  android:ems="10"
  android:inputType="number" >


  </EditText>

  <EditText
  android:id="@+id/editTextMob"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/EditTextCNIC"
  android:layout_alignTop="@+id/TextViewMob"
  android:ems="10"
  android:hint="Enter Ufone No. only"
  android:inputType="phone" />

  <Button
  android:id="@+id/buttonSubmit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignRight="@+id/EditTextKin"
  android:layout_marginBottom="43dp"
  android:layout_marginRight="26dp"
  android:text="Save and Open Account" />

  <ImageView
  android:id="@+id/imageView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentLeft="true"
  android:layout_marginLeft="19dp"
  android:src="@drawable/cr_l0_button" />

  <ListView
  android:id="@+id/listView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_above="@+id/EditTextKin"
  android:layout_alignParentLeft="true">


  </ListView>

</RelativeLayout>

</ScrollView>

i have also tried this: (may be there is problem in it )

         setListViewHeightBasedOnChildren(listviewattachment);//method call inonCreate  
    private  void setListViewHeightBasedOnChildren(ListView listView) {
    // TODO Auto-generated method stub
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) {
         // pre-condition
         return;
     }

     int totalHeight = 0;
     for (int i = 0; i < listAdapter.getCount(); i++) {
         View listItem = listAdapter.getView(i, null, listView);
         listItem.measure(0, 0);
         totalHeight += listItem.getMeasuredHeight();
     }

     ViewGroup.LayoutParams params = listView.getLayoutParams();
     params.height = totalHeight + (listView.getDividerHeight() *     (listAdapter.getCount() - 1));
     listView.setLayoutParams(params);

}
user2768215
  • 155
  • 2
  • 15
  • Similar question http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android – Piyush Oct 02 '13 at 07:31

2 Answers2

1

You shouldn't use a ListView inside a ScrollView because ListView class implements its own scrolling behavior. So there will be issues in receiving gesture events because they all are handled by the parent ScrollView. Maybe you could rethink/redesign your layout a bit in order to avoid this solution.

fasteque
  • 4,309
  • 8
  • 38
  • 50
0

If the number of views/fields is static, just remove the ListView and add the fields to the ScrollView child, which is not scrolling because you set a fixed height for the RelativeLayout; try setting just wrap_content

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="true">

Other tips.-

  • fill_parent is deprecated, use match_parent instead.
  • RelativeLayout has no orientation attribute, but actually you should replace the RelativeLayout for a LinearLayout vertical oriented, so you could remove all below/above atributes.
ssantos
  • 16,001
  • 7
  • 50
  • 70