Make Android View Scrollable both Horizontally and Vertically


If you want to have a Android Activity to be scrollable both horizontally and vertically be its any parent layout i.e LinearLayout, TableLayout, RelativeLayout or GridLayout, you can achieve it by making use of both HorizontalScrollView and ScrollView

Step 1: Choose your Parent layout. In the example I have choose a LinearLayout with with horizontal orientation.

Step 2: Add HorizontalScrollView as a sub-view with layout_width and layout_height as match_parent. You may add padding if you want.

Step 3: Within the HorizontalScrollView Add ScrollView as a sub-view with layout_width and layout_height as match_parent. You may add padding if you want.

Step 4: Now here you can all all the View elements that make up your Activity. To demonstrative i have added Two LinearLayouts with orientations vertical and horizontal respectively and added 10+ Button's to it.

Thats it! Try running the App and you would see that the View Scrolls both ways!

Source code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10px" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10px" >

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

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 1" />

                    <Button
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 2" />

                    <Button
                        android:id="@+id/button3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 3" />

                    <Button
                        android:id="@+id/button12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 4" />

                    <Button
                        android:id="@+id/button4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 5" />

                    <Button
                        android:id="@+id/button5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 6" />

                    <Button
                        android:id="@+id/button6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 7" />

                    <Button
                        android:id="@+id/button7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 8" />

                    <Button
                        android:id="@+id/button8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 9" />

                    <Button
                        android:id="@+id/button9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 10" />

                    <Button
                        android:id="@+id/button10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 11" />

                    <Button
                        android:id="@+id/button11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/button111"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 2" />

                    <Button
                        android:id="@+id/button21"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 3" />

                    <Button
                        android:id="@+id/button31"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 4" />

                    <Button
                        android:id="@+id/button121"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 5" />

                    <Button
                        android:id="@+id/button41"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 6" />

                    <Button
                        android:id="@+id/button51"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 7" />

                    <Button
                        android:id="@+id/button61"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 8" />

                    <Button
                        android:id="@+id/button71"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 9" />

                    <Button
                        android:id="@+id/button81"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 10" />

                    <Button
                        android:id="@+id/button91"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 11" />

                    <Button
                        android:id="@+id/button101"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 12" />

                    <Button
                        android:id="@+id/button1121"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button 13" />
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </HorizontalScrollView>

</LinearLayout>
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency