Android Horizontal ScrollView Tutorial With Example For Beginners

android gridview, android fragment tutorial, android scrollview, android horizontal scrollview, android fillviewport, android spinner

Warmest welcome to Android Horizontal ScrollView Tutorial With Example For Beginners.

I will explain about the definition and usage about the horizontal scroll view in this tutorial.

There are some cases where you need to show the icons or other things in the horizontal manner.

Now if the number of items are large then small screen of mobile device can not handle these items. Here, horizontal scroll view holds our hand.

For example, you are developing an app about the book store or library. Now you want to make book shelf in this app. Book shelf have horizontal design where you will show the cover page books in the horizontal manner.

Of course, there are much books in the every category. Here, you need to use the horizontal scroll view to show case book shelf with plenty of book cover pages.

A Horizontal scroll view is a sub class of Frame Layout meaning that you should place one child in it containing the entire contents to scroll. This child may itself be a layout manager with a complex hierarchy of objects.

Generally, developers prefer to use Linearlayout as an only child of the Horizontal Scroll View. Here, orientation of linearlayout is horizontal so that compiler place every child horizontally.

The TextView class also takes care of its own scrolling, so does not require a HorizontalScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

Using horizontalscrollview, you can enable horizontal scrolling only.

For vertical scrolling you should use Scroll View or ListView or RecyclerView Widgets.

XML Information

Following is the XML representation of the horizontalscrollview.

  <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

Following are the important properties of the horizontal scroll view.

1. android:layout_height

You can set the height of the horizontal scroll view by using this property.

To give fix height to the horizontal scroll view, you can use dp also.

2. android:layout_width 

It will define the width of the horizontal scroll view. Generally, you should give value as match_parent so that it reduce the complexity for different screen size.

But you can also give fix size in the dp.

3. android:id

Id will specify the unique identity to the scroll view.

When you want to access your horizontal scroll view in the java file, You will need this id in the findViewById() method.

This id should be unique in the whole android studio project to reduce complexity and data redundancy.

4. android:fillViewport

It defines whether the horizontal scrollview should stretch its content to fill the viewport.

This property accepts boolean value. If true then scroll view will stretch it’s content otherwise not.

For more details about this property, visit this tutorial.

Methods of Horizontal Scroll View

1. addView(View child)

Using the above method, you can add a child from the java class.

2. arrowScroll(int direction)

Handle scrolling in response to a left or right arrow click.

3. computeScroll()

Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.

4. fullScroll(int direction)

This method handles scrolling in response to a “home/end” shortcut press.

5. isFillViewport()

Indicates whether this HorizontalScrollView’s content is stretched to fill the viewport.

This method checks whether the fillViewPort option is enabled for horizontal scroll view or not.

6. onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.

7. requestChildFocus(View child, View focused)

Called when a child of this parent wants focus.

By default focus is hold by the horizontalscrollview.

8. scrollTo(int x, int y)

Set the scrolled position of your view.

This version also clamps the scrolling to the bounds of our child.

9. setFillViewport(boolean fillViewport)

Indicates this HorizontalScrollView whether it should stretch its content width to fill the viewport or not.

10. onSizeChanged(int w, int h, int oldw, int oldh)

This is called during layout when the size of this view has changed.

Android Horizontal ScrollView Example

Create a new and fresh project in the android studio.

While you are going through the process of preparing a brand new project in android studio, you will be asked to choose the type of the default activity.

Here, I recommend you to select Empty Activity. Empty activity does not write any source code automatically, so that we can have complete clear and empty project workspace.

Now in the new project, there are two automatically generated files. activity_main.xml and MainActiivity.java.

Step 1. Make XML file

In the activity_main.xml file, you need to add the below source code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#000"
                android:text="Button 1"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#e71313"
                android:text="Button 2"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#14e634"
                android:text="Button 3"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#dc12ea"
                android:text="Button 4"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#08e9b5"
                android:text="Button 5"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#000"
                android:text="Button 6"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#e2c90d"
                android:text="Button 7"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#bc10f1"
                android:text="Button 8"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#eda514"
                android:text="Button 9"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#13e8f7"
                android:text="Button 10"
                android:textColor="#fff"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="#000"
                android:text="Button 11"
                android:textColor="#fff"
                android:textStyle="bold" />

        </LinearLayout>

    </HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

Above file make a layout where compiler will put 11 buttons horizontally which you can scroll horizontally.

Root layout of this file is the constraint layout.

Constraint layout has only one child, which is the HorizontalScrollView. This tag is the main element, which will enable us to make our layout horizontal scrollable.

Now official documentation of HorizontalScrollView says that it should contain only one child.

So HorizontalScrollView also have only one child, which is Linearlayout.

This linearlayout contains the 11 buttons. Orientation property of linearlayout has the value “horizontal”.

Because of this “horizontal” value of orientation property, linearlayout will arrange all the eleven buttons in the horizontal manner.

If the value of orientation property is “vertical” then there is no meaning of using horizontal scroll view.

Step 2. Java Writings

Our main task in this project is to make horizontal scroll view.

We can achieve it from XML file activity_main.xml only.

So we do not need to do any programming logic in the JAVA file.

Hence, do not update anything in the MainActivity.java file.

Source code for MainActivity.java file is looking like the below

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);
    }
}

After this much work in this project, run it and you should get the below output

Another Example For Scroll View

Our first example was adding only buttons in the horizontal scroll view.

Now in this example, I will add images in the horizontal scroll view.

So again, make a new project in the android studio.

Again this time, you need select Empty activity as the default activity in this project.

Step 1. Updating activity_main.xml file

You will find activity_mian.xml file in this project also.

This time, source code for activity_mian.xml file is looking like the below

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="100dp">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/spider"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/hulk"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/captain"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/scarlet"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/blackp"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/widow"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/iron"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/spider"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:padding="5sp"
            android:src="@mipmap/spider"/>

    </LinearLayout>

</HorizontalScrollView>

The parent element of this file is the HorizontalScrollView.

As mentioned in the previous example, horizontal scroll view can have only one child tag so this parent element have one child as a linearlayout.

Now this linearlayout contains several number of imageviews.

All this imageviews have mages of different marvel heroes.

Orientation property for this linearlayout has the value “horizontal”. Hence, compiler will set every image horizontally.

You can scroll to view all the images. You can also add textview, button,imageview together to make horizontal scroll view something like you can find on the play store.

Play store shows us various apps which are horizontally scrollable along with the app icon and app name.

Step 2. Java Programme

Similar to previous example, we have written all the logic for horizontal scroll view in the activity_main.xml file.

So again, we should not touch MainActivity.java file.

This file may look like the below

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);
    }
}

The final outcome of this file may look like the below