4

I am trying to put a ListView below a ScrollView in android. I tried putting them inside a LineaLayout like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/frameLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ScrollView 
      android:id="@+id/marketDetailScrollView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
   ...
  </ScrollView>
  <ListView  android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:background="#FFF"/>
</LinearLayout>

and the ListView doesn't get shown. I also tried putting it inside a RelaviteLayout and still nothing. Can I somehow have a ListView under a ScrollView?

Just to add something. I don't want to split my screen so that I have a half with a ScrollView and another half with a ListView. I want the user to scroll down the ScrollView which apparently is bigger than the screen size and then the ListView should start

4
  • fix the height of scroll view.. like android:layout_height="100dip" Aug 20, 2013 at 9:44
  • you can have listview add the views inside scrollview as a header or footer to listview (without scrollview) or specify a fixed height of scroll view and add listview below it Aug 20, 2013 at 9:44
  • 1
    In your scrollview, android:layout_height="fill_parent" means that your scrollview will take all the space to the bottom, so there is no more space for your ListView.
    – Damien R.
    Aug 20, 2013 at 9:46
  • You are using LinearLayout, and you place your ListView below ScrollView, plus you set the width and height of the ScrollView are fill_parent so the ScrollView will override and gain the display of the ListView, if you want your ListView appears, set a specific size to your ScrollView, 200dp for example. Hope this helps. Aug 20, 2013 at 9:48

5 Answers 5

10

I would recommend you to put the ScrollView Content as HeaderView in the ListView or do you explicitly want to have two separated scrollable areas on screen?

Example for putting the content of the scroll view in the list view as header (one single scrollable area):

public void onCreate(Bundle s){
    setContentView(R.id.yourLayout);

    ListView listView = (ListView) findViewById(R.id.listView);

    // Set the adapter before the header, because older 
    // Android version may have problems if not

    listView.setAdapter(new YourAdapter());

    // Add the header
    View header = inflater.inflate(
            R.layout.you_layout_that_was_in_scrollview_before, null, false); 

    listView.addHeaderView(header);

}

The layout of the activity would look like that:

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

    <ListView  android:id="@+id/listView" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:background="#FFF"/>
</LinearLayout>

If you want two scrollable areas, you should work with layout weight:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView 
  android:id="@+id/marketDetailScrollView"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" >
  <!-- ScrollViewContent -->
</ScrollView>
<ListView  android:id="@android:id/list" 
android:layout_height="0dp" 
android:layout_width="match_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>
5
  • No I don't. So by putting is as a headerview will just create a scrollable view?
    – alecnash
    Aug 20, 2013 at 10:10
  • Yes ... that would create one single scrollable area ... I will update my answer and add an example
    – sockeqwe
    Aug 20, 2013 at 10:12
  • I still have two scrollable areas
    – alecnash
    Aug 20, 2013 at 10:17
  • Setting adapter before header will make android crash. Except from that part, this is great.
    – Warpzit
    May 24, 2014 at 11:53
  • It depends on Android API. It has changed with android 4.1 (i guess). I didnt remember what was the correct way, but if it crashs after setting the adaper, then set it before setting the adapter to the listview or vice versa. I guess setting the header before setting the adapter to the list view should work on all android platforms.
    – sockeqwe
    May 24, 2014 at 17:52
0

Fill parent wont work for you, change it to wrap content, and give layout_weight 1 to scroll view also, then it may work for you

0

Normally it is not good to put listview in scrollview. but if in case it is required, you need to set the height of listview. Follow the link below to calculate the height of listview for fitting in scrollview.

http://www.jiramot.info/android-listview-in-scrollview-problem

Using this way you can achieve what you want

2
  • its not in scroll view its under scrollview
    – alecnash
    Aug 20, 2013 at 9:52
  • Ohh okay. In that case just use weight property and set weight for scrollview and listview in your linearlayout. It should work
    – Sushil
    Aug 20, 2013 at 9:55
0

You have to set the the height of the content of scrollview and its element to 'wrap content'. XML will be like this:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView 
  android:id="@+id/marketDetailScrollView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >
...(height will be wrap_content for inner element as well)
</ScrollView>
<ListView  android:id="@android:id/list" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>

This will might help you..

4
  • This is may be because of your inner content of ScrollView. Can you tell me what is your inner content of scrollView?
    – Avijit
    Aug 20, 2013 at 10:02
  • What has the inner content to do with that? The scrollview is bigger than the screen
    – alecnash
    Aug 20, 2013 at 10:07
  • Check that any of the content's height is fill_parent/match parent or not. My code is working fine. Just check that it is working or not.
    – Avijit
    Aug 20, 2013 at 10:14
  • OP will be notified when u have posted an answer... no need to post comment under question everytime u answers...
    – Avadhani Y
    Aug 26, 2013 at 7:06
0

I think this is near to what you are looking for. I give you this code code but i have to tell that putting a ListView in a ScrollView is a bad-practice and you should not use this solution. You should work on something more correct and with no scroll view within scroll view.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/scrollViewContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/marketDetailScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/scrollViewContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <!-- the views contained in your ScrollView goes here -->
            </LinearLayout>
        </ScrollView>

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFF" />
    </LinearLayout>

</ScrollView>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.