android-studio
  1. android-studio-horizontal-scrollview

Android Studio Horizontal ScrollView

The Horizontal ScrollView widget is used in Android Studio to create a horizontal scrollable view for displaying a set of images or other views horizontally. It is a popular widget for creating Instagram-like pages or for displaying multiple images side by side.

Syntax

The Horizontal ScrollView in Android Studio has the following syntax:

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="200dp" >

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      android:padding="10dp">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image1"/>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image2"/>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image3"/>

    </LinearLayout>
    
</HorizontalScrollView>

Example

Here's an example of how to create a Horizontal ScrollView in Android Studio:

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="200dp" >

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      android:padding="10dp">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image1"/>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image2"/>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image3"/>

    </LinearLayout>
    
</HorizontalScrollView>

Output

The above example will display a horizontal scrollable view with three images side by side.

Explanation

The HorizontalScrollView widget is used to create a horizontal scrollable view. In the above example, we have used a LinearLayout inside the HorizontalScrollView to hold our images. We have set the android:orientation attribute of this LinearLayout to horizontal to display the images horizontally.

We have also set the android:layout_width attribute of the LinearLayout to wrap_content and android:layout_height to match_parent. This will allow the LinearLayout to wrap the content of the images and extend its height to that of the whole screen.

We have added the ImageView tags to display three images side by side. We have set the android:layout_width and android:layout_height attributes of these images to a fixed size of 100dp to ensure the images are of equal size.

Use

The Horizontal ScrollView widget is used in various Android Studio projects where there is a need for a horizontal scrollable view, such as displaying a set of images, for creating e-commerce apps, and displaying Instagram-like pages.

Important Points

  • The android:orientation attribute of the LinearLayout inside the HorizontalScrollView must be set to horizontal.
  • The android:layout_width attribute of the LinearLayout inside the HorizontalScrollView must be set to wrap_content.
  • The width and height of the images inside the LinearLayout must be set to a fixed size to display them side by side.

Summary

The Horizontal ScrollView widget is a useful tool for displaying multiple images side by side or creating a horizontal scrollable view for any purpose in an Android Studio project. It allows the user to swipe the view horizontally to see more content.

Published on: