Horizontal and Vertical Scrolling in Android

How to Perform Horizontal and Vertical Scrolling in android?

I just thought of posting this because beginners usually find issues in performing scrolling both ways(vertical and horizontal) as I did being one.

ScrollView can on do only vertical scrolling. Inorder to perform horizontal scrolling use HorizontalScrollView.

Here is how we can perform scrolling-horizontal and vertical:

Lets look at what I’ve done here.I’ve made the ScrollView which performs the vertical scrolling to enclose the different HorizontalScrollViews (as the name hints does the horizontal scrolling),Layouts(here I used Linear Layout),TextViews,ImageViews etc which I’ve used in my piece of work ,so that the entire elements will automatically help us in scrolling downwards or upwards as the screen ends.Here, when you look at the xml code below, you will notice that each set of elements under different HorizontalScrollView can be scrolled horizontally.

Here I have used LinearLayouts.Just below ScrollView , you can see a LinearLayout with orientation set as “vertical” which holds the entire set of HorizontalScrollViews and the LinearLayouts(with  “horizontalorientation)in between each HorizontalScrollView.

Just look at what I’m trying to explain using ,say, my own version of algorithm :

ScrollView(vericalscrolling-scrolling up and down)

LinearLayoutvertical(places elements between the layout one below the other)

HorizontalScrollView(scrolling sideways)

LinearLayouthorizontal(places elements between the layout one beside the other)

Here, elements will be placed horizontally.

closing tag for LinearLayouthorizontal

closing tag for HorizontalScrollView

{enclose n number of elements as your application demands}

closing Linear Layoutvertical

finally closing ScrollView

Now just go through the code below and try by yourself.Its simple and interesting too.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideInset"
android:fadeScrollbars="true"
android:scrollbarSize="10dip"
android:fitsSystemWindows="true" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/tt"  >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text one.."
android:textColor="#000000"
android:background="#ffffff"
android:textSize="20dip"/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideInset"
android:fadeScrollbars="true"
android:scrollbarSize="10dip"
android:fitsSystemWindows="true"   >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:src="@drawable/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/fgj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/ggcgh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/thhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:src="@drawable/fhg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:src="@drawable/jgeraej"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:src="@drawable/jhdfdjh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#000000"
android:text="Text two.."
android:textSize="20dip"/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideInset"
android:fadeScrollbars="true"
android:scrollbarSize="10dip"
android:fitsSystemWindows="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:src="@drawable/thhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/fgj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/hghhgh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:src="@drawable/jgeraej"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/fhg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:src="@drawable/thhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>

The output will look like this:

Horizontal and Vertical Scrolling

, , , , , , , , , , ,

  1. #1 by Salome Bulleri on March 9, 2012 - 6:28 am

    Very nice post, I certainly love this website, keep on it.

  2. #2 by lou on December 12, 2012 - 6:29 am

    this is great. but how to get the scrolling to be exclusive. if i’m scrolling horizontally, keep scrolling horizontally until i let go. dont let the user scroll vertically until the horizontal scroll is finished.

  3. #3 by Pavan Deshpande on August 5, 2013 - 4:44 pm

    Hi thats very nice tutorial on scrollview concept in android you can also check the one at
    android scrollview

    android styled scrollview

  4. #4 by soubhagya on March 7, 2014 - 11:50 am

    u just make the day for me …thank you very much

  5. #5 by mostafa901 on September 27, 2014 - 5:17 pm

    Thanks for this information.

  6. #6 by Abhishek on April 6, 2015 - 1:23 pm

    Thanx alot

  7. #7 by krishna on April 16, 2015 - 10:39 am

    very nice post ..

  8. #8 by Khalil on May 15, 2015 - 2:48 pm

    Is it possible to scroll horizontal and vertical at the same time ?

Leave a comment