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
?
-
4possible duplicate of [How to make a LinearLayout scrollable](http://stackoverflow.com/questions/3416689/how-to-make-a-linearlayout-scrollable) – Narkha Jan 16 '15 at 22:37
10 Answers
Wrap the linear layout with a <ScrollView>
<?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.

- 3,797
- 3
- 41
- 39

- 27,363
- 32
- 109
- 125
-
1@DanielMagnusson Link still works here... here's a video guide: http://www.youtube.com/watch?v=kNX996ZZ2CI – Bryan Denny Aug 06 '12 at 13:29
-
35
-
1@Lawrence No, it isn't necessary, but can depend on what the rest of your view looks like. – Bryan Denny Jun 26 '13 at 20:50
-
1If 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. – Niels Nov 19 '15 at 06:48
-
3
-
@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. – Fernando Torres Oct 21 '19 at 02:08
-
@FernandoUrban I think you're referring to the inner `LinearLayout`. I was referring to the outer (top-level) one. – Lawrence Kesteloot Oct 21 '19 at 19:42
-
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher) – majurageerthan Mar 08 '20 at 05:40
-
-
Note: This only allows for vertical scrolling. To scroll horizontally, you must use a [HorizontalScrollView](https://stackoverflow.com/questions/12317648/making-a-linear-layout-horizontally-scrollable). – Mr-IDE Sep 15 '22 at 09:25
<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>

- 4,587
- 6
- 31
- 60

- 16,089
- 25
- 97
- 143
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>

- 257
- 3
- 11
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.

- 5,339
- 2
- 48
- 48

- 41
- 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. – Caltor Apr 15 '19 at 09:43
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>

- 8,890
- 6
- 44
- 59
you need to use the following attribute and enclose it within the linear layout
<LinearLayout ...>
<scrollView ...>
</scrollView>
</LinearLayout>

- 113
- 3
- 12
<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>

- 5,776
- 3
- 34
- 48

- 11
- 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.

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

- 595
- 3
- 13
You can add an atrribute in linearLayout : android:scrollbars="vertical"

- 24,084
- 23
- 95
- 173

- 53
-
4This controls the visibility of the scrollbars only. But it does not creates a scrollview for you. – Botond Kopacz May 05 '15 at 18:17