How to create a grid layout with horizontal scroll?
- March 1, 2016
- Android App Development
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size.
The code below will let you build a grid view with horizontal scroll as shown below
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/toast_layout_root”
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”8dp”
android:background=”#FFF”>
<HorizontalScrollView xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/container_scroll_view”
android:layout_width=”wrap_content”
android:layout_height=”match_parent”
android:background=”#ffffff”>
<GridLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/GridLayout1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:columnCount=”5″
android:rowCount=”6″
android:orientation=”horizontal”
tools:context=”.GridXMLActivity”>
<RelativeLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
<View android:layout_marginTop=”20dp”
android:layout_marginLeft=”10dp”
android:layout_marginRight=”5dp”
android:layout_width=”150dp”
android:layout_height=”150dp”
android:paddingLeft=”1dp”
android:paddingRight=”1dp”
android:id=”@+id/View1″
android:layout_row=”0″
android:layout_column=”0″
android:background=”#f5f915″/>
<ImageView
android:layout_marginTop=”10dp”
android:layout_marginLeft=”10dp”
android:layout_marginRight=”5dp”
android:src=”@drawable/sample_1″
android:layout_width=”100dp”
android:layout_height=”100dp”
android:layout_alignTop=”@id/View1″
android:layout_centerInParent=”true”
android:paddingLeft=”1dp”
android:paddingRight=”1dp”
android:id=”@+id/imageView1″
/>
</RelativeLayout>
</GridLayout>
</HorizontalScrollView>
</LinearLayout>
Horizontal scroll view
A HorizontalScrollView is a FrameLayout, meaning 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. A child that is often used is a LinearLayout in a horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through.
Relative layout
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements or in positions relative to the parent RelativeLayout area.
Image View
The ImageView class can load images from various sources, takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
The code snippet is as shown below:
Your screen will now look like this
For digital marketing services, to increase sales online or offline, 🌐visit: https://www.aimglobal.mobi/
Connect with us on:
📸Instagram: https://instagram.com/aimglobalmobi?igshid=o6atyi8pfz74
About us and this blog
We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.
Request a free quote
We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.
Subscribe to our newsletter!
More from our blog
See all postsRecent Posts
- Top 5 Marketing Channels: Examples With Pros & Cons February 23, 2023
- The 8-Step Checklist for Launching an influencer Marketing February 23, 2023
- How to Build an Email List Through Social Media January 31, 2023