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:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button5" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override 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
Amazing 😊
Can you provide the same implementation of expandable list view with horizontal scrolling childrens. That would be amazing.