Search code examples
androidscrollable

Add programmatically a scrollable linear layout


I've this layout file and the java code.

I need to create a horizontally scrollable linear layout to scroll over the buttons. In this way it doesn't scroll at all.

<ScrollView
    android:id="@+id/questionListSV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/questionList_LL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>
</ScrollView>
for (int i =0; i<maxQuestionNo;i++) {
    Button qbtn = new Button(mContext);
    qbtn.setText(String.valueOf(i + 1));
    ll.addView(qbtn);
}

Solution

  • You need to use HorizontalScrollView instead of ScrollView

     <HorizontalScrollView
        android:id="@+id/questionListSV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true">
    
        <LinearLayout
            android:id="@+id/questionList_LL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        </LinearLayout>
    </HorizontalScrollView>