1

This my xml file that shows listview under linerlayout and scrollview. How can I make my listview scroll? Because what I wanted is the background image under the linearlayout is scrolling too. But whenever I wanted to scroll the listview it rather scroll the whole layout.

<ScrollView 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"
    tools:context=".MainScreenEntered" 
   >

     <LinearLayout
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/mainscreen"
         >  

     <EditText
        android:id="@+id/txtSearch"
        android:layout_width="220dp"
        android:layout_height="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="130dp"
        android:background="@drawable/white"
        android:height="10dp"
        android:singleLine="true"
        android:textSize="15sp"
        android:hint="@string/search"
      />    

     <Button
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="250dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-41dp"
        android:background="@drawable/btnsearch"
      />

    <ListView
        android:id="@+id/lvEntries"
        android:layout_width="255dp"
        android:layout_height="300dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="41dp"
        />

    <Button
        android:layout_width="280dp"
        android:layout_height="55dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="21dp"
        android:background="@drawable/btnaddentry"
        android:onClick="goToAddEntryScreen"
      />

     </LinearLayout>
</ScrollView>
akoDwin
  • 139
  • 2
  • 2
  • 14
  • goto http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing it works – Pdee As-Diddy Sep 01 '14 at 00:50

1 Answers1

1

You should not put ListView under a ScrollView. ListView scrolls by itself. Instead you can add views as a header or footer to listview.

Quoting docs

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

You can also use a RelativeLayout. Only ListView Scrolls the EditText and Buttons do not scroll. Modify the below Accordingly

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="81dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ListView
        android:id="@+id/listView1"
        android:layout_above="@id/button1"
        android:layout_below="@id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

    >
    </ListView>

</RelativeLayout>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    um. If I would not put a listview in a scrollview, how can I make my background scroll? is there any solution to scroll the background and the layout in either linearlayout or relativelayout? – akoDwin Mar 08 '14 at 03:35
  • @user3375492 why would you want to make the background scroll? check the edit add background to `RelativeLayout`. – Raghunandan Mar 08 '14 at 03:36
  • because i have a lot of views and it doesn't fit in my screen (which is a HVGA Slider). When I used the background in relativelayout it become distorted. It doesn't show the height I wanted. – akoDwin Mar 08 '14 at 03:48
  • @user3375492 add those views as a header and footer to listview. ListView scrolls by itself. what is that you want to achieve. Why do you want those so many views when it does not fit into screen. Listview is the option not scrollview – Raghunandan Mar 08 '14 at 03:50
  • oh. I think I will just simplify my design and fit the background to the size of the device. Thank you anyway. :) – akoDwin Mar 08 '14 at 04:34