Horizontal Scroll View Code Snippet for Android Studio

 

Welcome. In this tutorial you will get to know how to add a horizontal scroll view in your android application.

Android Horizontal Scroll View Example

A horizontal scroll view is a type of Frame Layout in android studio, which means you can place one layout or child inside it which will contain the contents to scroll. You can put any kind of layout inside a horizontal scroll view which, will contain different objects in it. Linear Layout is the most common type of layout that is added as a child. You can place buttons, images, and any other UI elements as desired inside Horizontal scroll view. Whatever you will put inside horizontal Scroll view, you can scroll it sideways for navigation. But this type of scroll view is rarely used these days and people instead prefer just the normal vertical scroll view.

But remember, Horizontal scroll view will only support horizontal scrolling, but not vertical scrolling. If you need to add vertical scrolling then you should use either ScrollView or ListView.

 

Inserting the horizontal scroll view in your layout

To insert a horizontal scroll view,just add these lines in your XML layout:-

Note that you will have to add your own child layout on the place mentioned above like any LinearLayout and add various other components in it to perform horizontal scrolling.

 

For example purpose, a gif is shown below:-

Horizontal Scroll Code Snippet in Android Studio

 

The Complete Code Snippet for above example is given below:-

Java (MainActivity.java)

 

XML (activity_main.xml)

 

 

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project.

 

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.android.com/reference/android/widget/HorizontalScrollView

Comment down below if you are facing any problems.

 

Also Read:-

Leave a Reply

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