How do you make a LinearLayout scrollable?
Asked Answered
A

10

274

I have lot of items on the screen and I need to use the scrollbar so the user can scroll down. However, the scroll is either not visible or it's not working. How is it possible to add a scrollbar to a LinearLayout?

Alejandroalejo answered 29/10, 2010 at 20:12 Comment(1)
possible duplicate of How to make a LinearLayout scrollableCaril
M
491

Wrap the linear layout with a <ScrollView>

See here for an example:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       xmlns:android="http://schemas.android.com/apk/res/android">
       <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
                  <!-- Content here -->
            </LinearLayout>
      </ScrollView>
 </LinearLayout>

Note: fill_parent is deprecated and renamed to match_parent in API Level 8 and higher.

Marven answered 29/10, 2010 at 20:17 Comment(10)
@DanielMagnusson Link still works here... here's a video guide: youtube.com/watch?v=kNX996ZZ2CIMarven
I don't think you need the outer LinearLayout.Effusion
@Lawrence No, it isn't necessary, but can depend on what the rest of your view looks like.Marven
If you have only the Scrollview in the top linearlayout, you get the warning "This ScrollView layout or its LinearLayout parent is useless; transfer the background attribute to the other view" which means to say it's pointless to have a linearlayout with only one item.Stockbroker
and some importatn notice: ScrollView must have only 1 childMonohydroxy
@LawrenceKesteloot That's what i tought but if you're on api >= 28 ther's a syntax error because ScrollView tag just can render one child into in, so that's why you close into a LinearLayout tag to skip this thing.Dig
@FernandoUrban I think you're referring to the inner LinearLayout. I was referring to the outer (top-level) one.Effusion
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher)Kenning
Use androidx.core.widget.NestedScrollView instead of Scroll ViewCatina
Note: This only allows for vertical scrolling. To scroll horizontally, you must use a HorizontalScrollView.Venable
S
154
<ScrollView 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/scroll" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

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

 </ScrollView>
Seizing answered 30/10, 2010 at 4:16 Comment(0)
R
8

You need to wrap your linear layout with a scroll view

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

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


    </LinearLayout>
</ScrollView>
Rriocard answered 6/11, 2020 at 7:3 Comment(0)
P
4

Here is how I did it by trial and error.

ScrollView - (the outer wrapper).

    LinearLayout (child-1).

        LinearLayout (child-1a).

        LinearLayout (child-1b).

Since ScrollView can have only one child, that child is a linear layout. Then all the other layout types occur in the first linear layout. I haven't tried to include a relative layout yet, but they drive me nuts so I will wait until my sanity returns.

Photographic answered 28/3, 2013 at 0:18 Comment(1)
Could you fix the formatting please. Your first line has been missed from the code section and the site won't let me fix it as it is too trivial an edit.Hexose
C
4

This can be done using the tag <ScrollView>. For ScrollView, one thing you have to remind that, ScrollView must have a single child.

If you want your full layout to be scrollable then add <ScrollView> at the top. Check the example given below.

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

    <LinearLayout 
        android:id="@+id/container" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            <!-- Content here -->
    </LinearLayout>

</ScrollView>

But if you want some part of your layout to be scrollable then add <ScrollView> within that part. Check the example given below.

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
                <!-- Content here -->
        </LinearLayout>

    </ScrollView>

</LinearLayout>
Cordwain answered 9/6, 2017 at 3:36 Comment(0)
C
2

you need to use the following attribute and enclose it within the linear layout

<LinearLayout ...>
<scrollView ...> 

</scrollView>
</LinearLayout>
Cuspidation answered 3/2, 2018 at 19:3 Comment(0)
S
1
 <LinearLayout 
        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"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <---------Content Here --------------->
            </LinearLayout>
       </ScrollView>
    </LinearLayout>
Suchta answered 7/5, 2016 at 19:7 Comment(0)
S
1

You need to place ScrollView as the first child of Layout file and now put your linearlayout inside it. Now, android will decide on the basis of content and device size available whether to show a scrollable or not.

Make sure linearlayout has no sibling because ScrollView can not have more than one child.

Sesquipedalian answered 2/1, 2017 at 9:1 Comment(0)
G
0

Whenever you wanted to make a layout scrollable, you can use <ScrollView> With a layout or component in it.

Guzman answered 6/11, 2020 at 7:33 Comment(0)
E
-29

You can add an atrribute in linearLayout : android:scrollbars="vertical"

Erena answered 30/10, 2010 at 4:18 Comment(1)
This controls the visibility of the scrollbars only. But it does not creates a scrollview for you.Nitrosyl

© 2022 - 2024 — McMap. All rights reserved.