Make Linearlayout scrollable without using Scrollview

10,628

Solution 1

Actually after doing some research, I come up with a solution for this problem:

At first I want to explain the problem in a very simple way.

  1. LinearLayout will be scrollable. To do this we can use ScrollView but sometimes we need to use ListView inside LinearLayout.
  2. We know that inside ScrollView we cannot use another scrollview like ListView

How to solve that?

ListView is scrollable inherently so we can add header and footer in the ListView. As a conclusion:

  1. Create Layout header.xml and footer.xml and list.xml
  2. Find the ListView reference from list.xml in the main activity and dynamically add header and footer in the ListView reference.

Solution 2

If you are only using a listview inside a linear layout, then you don't need to use scrollview.Because ListView is scrollable by default.But if you have other components as well then you can separate those in another scrollview.Make sure that ScrollView only uses one direct child layout. Below is a sample code.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff"> 

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"/> 

</LinearLayout>
Share:
10,628
androidcodehunter
Author by

androidcodehunter

Working at VocaPower as an android developer. My daily activity is to implement new feature, improve efficiency, fix bug and checkout different modern architecture for android. The app I am currently working is VocaPower – Mnemonic Dictionary for Vocabulary Your feedback will be accepted promptly and implemented in my app.

Updated on June 05, 2022

Comments

  • androidcodehunter
    androidcodehunter almost 2 years

    I have a Linearlayout and I want to make it scrollable without using ScrollView. Is it possible. Any suggestions will be appreciated. Here's the detail: If I wrap the LinearLayout using ScrollView, it is ok but when I used a ListView inside LinearLayout(because it is my clients requirement), it said do not use ListView inside ScrollView. I have to show 50 product list using ListView and I have to put this ListView inside LinearLayout and at the same time whole layout will be scrollable. Is it possible. Here is the skeleton:

    <LinearLayout>
      <RelativeLayout>
        <LinearLayout> 
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <TextView>
        </LinearLayout>
        <LinearLayout>
            <ListView>
        </LinearLayout> 
      </RelativeLayout>
    </LinearLayout>
    

    Important: See the ListView where I want to add 50 list item. So how can I make this total LinearLayout scrollable.

  • androidcodehunter
    androidcodehunter about 11 years
    Actually I have to add 50 product list in the ListView and the listView will put inside the LinearLayout. I did it but the problem is when the list of items exceed the device size, the LinearLayout did not expand but I need to expand the LinearLayout as the ListView expanded.