Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add scrollbars to a LinearLayout in Android?

How do I add scrollbars to a view in Android?

I tried by adding android:scrollbars:"vertical" to the LinearLayout in my layout XML file but it is not working.

I thought scrollbars were drawn by default in Android but it does not seem that way. It seems we have to draw it ourselves - how do I do this?

like image 849
Angel Avatar asked Apr 14 '11 09:04

Angel


People also ask

Is linear layout scrollable android?

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.

How do I create a ScrollView with Textview and with a LinearLayout?

When adding a LinearLayout inside a ScrollView , use match_parent for the LinearLayout android:layout_width attribute to match the width of the parent ScrollView , and use wrap_content for the LinearLayout android:layout_height attribute to make it only large enough to enclose its contents.


2 Answers

You cannot add scrollbars to a LinearLayout because it is not a scrollable container.

Only scrollable containers such as ScrollView, HorizontalScrollView, ListView, GridView, ExpandableListView show scrollbars.

I suggest you place your LinearLayout inside a ScrollView which will by default show vertical scrollbars if there is enough content to scroll.

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <!-- Your content goes here -->   

    </LinearLayout>
</ScrollView>

If you want the vertical scrollbar to always be shown, then add android:scrollbarAlwaysDrawVerticalTrack="true" to your ScrollView. Note the height of the LinearLayout is set to wrap_content - this means the height of the LinearLayout can be larger than that of the ScrollView if there is enough content - in that case you will be able to scroll your LinearLayout up and down.

like image 57
Joseph Earl Avatar answered Sep 21 '22 00:09

Joseph Earl


You can't add a scrollbar to a widget that way. You can wrap your widget inside a ScrollView. Here's a simple example:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt"/>
    </LinearLayout>
</ScrollView>

If you want to do it in code:

ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);
like image 35
Rajath Avatar answered Sep 17 '22 00:09

Rajath