Android HorizontalScrollView Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android horizontalScrollView widget in any android application. We will also go through different attributes of horizontalScrollView widget that can be used to customise it.

Output

Tutorialwing Android HorizontalScrollView Output Android HorizontalScrollView Tutorial With Example

Tutorialwing Android HorizontalScrollView Output

Getting Started

HorizontalScrollView widget can be defined as below –

HorizontalScrollView is widget, a subclass of FrameLayout, that acts as container for the layout to be scrolled horizontally. It can contain only one direct child. So, if you want to define more child views inside horizontalScrollView, you must define a viewGroup (i.e. LinearLayout, RelativeLayout etc.) as a direct child of horizontalScrollView. Then, you can place other views inside this viewGroup. It supports only horizontal scrolling. So, if you want vertical scrolling, you can use ScrollView widget

Attributes of Android HorizontalScrollView Widget

Attributes of horizontalScrollView are same as ScrollView. So, You can check attributes of scrollView to get different attributes of horizontalScrollView.




Example of Android HorizontalScrollView Widget

At first, we will create android application. Then, we will use horizontalScrollView widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as HorizontalScrollView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use horizontalScrollView widget in the application.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">HorizontalScrollView</string>
	<string name="no_image">No Image</string>
</resources>

3. Download Drawable Resources Needed

You will need images, stored in res/drawable folder, to be used in the application. These drawable images will be used by views inside horizontalScrollView in the application.

4. Use HorizontalScrollView Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<LinearLayout
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginTop="20dp"
		android:orientation="horizontal">

		<ImageView
			android:id="@+id/image1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginLeft="20dp"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/guava"/>

		<ImageView
			android:id="@+id/image2"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/jackfruit"/>

		<ImageView
			android:id="@+id/image3"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/mix_fruit"/>

		<ImageView
			android:id="@+id/image4"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/pomegranate"/>

		<ImageView
			android:id="@+id/image5"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/strawberry"/>

		<ImageView
			android:id="@+id/image6"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginRight="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/zespri_kiwi"/>

	</LinearLayout>
</HorizontalScrollView>

In activity_main.xml file, we have defined horizontalScrollView. Then, we have defined views inside this widget. Note that there is only one direct child, i.e. LinearLayout, of horizontalScrollView. All the imageViews have been defined inside this linearLayout.

5. Access HorizontalScrollView Widget in java file

Open src/main/java/com.tutorialwing.horizontalscrollview/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.horizontalscrollview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

We have not modified anything in MainActivity file. So, it is as it was after creating project.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.horizontalscrollview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of tutorial on Android HorizontalScrollView widget.

Leave a Reply