Home » Android » core » ui » HorizontalScrollView » Android HorizontalScrollView Example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Android HorizontalScrollView Example

In this example we are going to see how to use HorizontalScrollView in an Android Application. The HorizontalScrollView is like a ScrollView but with horizontal orientation. It is very useful because it is not very usefull to scoll up and down the whole screen of you application. You can use a small portions of the screen as HorizontalScrollView with scrolling items and have the main screen stable most of the time.

For this tutorial, we will use the following tools in a Windows 64-bit platform:

  • JDK 1.7
  • Eclipse 4.2 Juno
  • Android SKD 4.2

1. Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project. You have to specify the Application Name, the Project Name and the Package name in the appropriate text fields and then click Next.


In the next window make sure the “Create activity” option is selected in order to create a new activity for your project, and click Next. This is optional as you can create a new activity after creating the project, but you can do it all in one step.


Select “BlankActivity” and click Next.


You will be asked to specify some information about the new activity.  In the Layout Name text field you have to specify the name of the file that will contain the layout description of your app. In our case the file res/layout/main.xml will be created. Then, click Finish.


2. Create the main layout of the Application

Open res/layout/main.xml file :


And paste the following code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:layout_marginTop="50dp" >

            android:orientation="horizontal" >

                android:text="Button1" />

                android:text="Button2" />

                android:text="Button3" />

                android:text="Button4" />

                android:text="Button5" />



Now you may open the Graphical layout editor to preview the User Interface you created:


3. Code

Now we have to write the code of the application. Use the Package Explorer to navigate to the Java file of the Activity you’ve created:


The code of this tutorial is pretty much self explanatory:

package com.javacodegeeks.android.androidhorizontalscrollview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;


4. Run the application

This is the main screen of our Application:


Now, when you scroll the button bar …:


Download Eclipse Project

This was an Android OnClickListener Example. Download the Eclipse Project of this tutorial: AndroidHorizontalScrollView.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.