Android Horizontal ScrollView Tutorial With Example For Beginners

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.

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

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

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

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

The final outcome of this file may look like the below

 

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *