Android Horizontal ScrollView Example

Android Horizontal ScrollView Example: Horizontal Scroll View In Android

A horizontal ScrollView is a layout container that can be scrolled Horizontally by a user. So it can scroll both left to right, or right to left. Let’s understand it with an Android Horizontal ScrollView Example.

 

 

Let’s build a custom app with Android Horizontal Scrollview Example

1- So open your Android Studio and build a new Android Studio Project.

2- Name your Project as Horizontal ScrollView. Also, you can give whatever name you like.

3- Then, first of all we are going to make a separate Layout file for a single Item in our Horizontal Scroll View. We make a separate Layout file and name it as text.xml.

4- So in your text.xml file first thing here is that you change your default Layout file from Constraint Layout to Linear Layout.

5- After changing to Linear Layout then we will take an ImageView with a 100dp height and width. ImageView will display our images and TextView will show the name of each item in the scrollable list.

6- In your text.xml file add the code given below. Then the final text.xml is like this:

 

Android Scrollview Horizontal 

text.xml

 

 

7- Here, we complete the Custom appearance of the single Item in our Horizontal Scroll View.

8- In our activity_main.xml file we will design our Horizontal Scroll View with a Linear Layout to display our Items. We will set the orientation of out HorizontalScrollView and LinearLayout to horizontal. After that, the final activity_main.xml file will look like this.

 

Android Horizontal Scrollview

activity_main.xml

 

 

9- Now, We are done with our Single Item’s appearance and Horizontal ScrollView Part.

10- We will now take two arrays, one for the texts and then one for the images because we have to bind our view in our Linear Layout inside the Horizontal Scroll View.

11- With the help of a for loop we will set the name and image of each item in our list.

12- Finally, our MainActivity.java code would be like this.

 

Android Horizontal Scrollview

                                                 MainActivity.java     

                                                  

 

So, this is it for our Horizontal Scrollview in Android. I hope now you understand the ScrollView Android example in a better way and if you have any questions you can ask below in the comments.

OUTPUT

 

Android Horizontal ScrollView Example
Android Horizontal ScrollView Example

 

Android Horizontal ScrollView Example

 

Android Horizontal ScrollView Example
Android Horizontal Scrollview

 

 

People Also Read

 

 

Leave a Comment

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

error: Content is protected !!