How to Disable GridView Scrolling in Android?
Last Updated :
29 Jul, 2021
A GridView is a ViewGroup that can display data from a list of objects or databases in a grid-like structure consisting of rows and columns. Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridView by default is set to enabled.
However, in this article, we will show to disable the scrolling ability of GridView.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Create this simple GridView in the layout.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Step 3: Create an Adapter for the Grid View (MyGridViewAdapter.kt)
We have to create an adapter to send data (Images in our case) to the Grid View. So, create a new class and give it a relevant name. To see how we added these photos, refer to Step 6.
Kotlin
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.BaseAdapter
import android.widget.ImageView
// Array of Images,
// we used GeeksforGeeks logo
private val myImages = arrayOf(
R.drawable.logo,
R.drawable.logo,
R.drawable.logo,
R.drawable.logo,
R.drawable.logo,
R.drawable.logo,
R.drawable.logo,
)
class MyGridViewAdapter constructor(c:Context): BaseAdapter() {
private val context: Context = c
override fun getCount(): Int {
return myImages.size
}
override fun getItem(position: Int): Any? {
return null
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val imageView: ImageView
if (convertView == null) {
imageView = ImageView(context)
imageView.layoutParams = AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
imageView.setPadding(30, 30, 30, 30)
}
else {
imageView = convertView as ImageView
}
imageView.setImageResource(myImages[position])
return imageView
}
}
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Link the Adapter to the Grid View in the Main code (MainActivity.kt). Comments are added inside the code to understand the code in more detail.
Kotlin
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declare the GridView from the layout
val gridView = findViewById<GridView>(R.id.gridLayout)
// Specify number of columns
gridView.numColumns = 1
// Setting the grid view
// adapter as MyGridViewAdapter
gridView.adapter = MyGridViewAdapter(this)
}
}
Output: Now run the application
You can see that we are able to scroll the GridView vertically.
Step 5: Add this code in the Main code to disable the scrolling (MainActivity.kt)
Kotlin
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val gridView = findViewById<GridView>(R.id.gridLayout)
gridView.numColumns = 1
gridView.adapter = MyGridViewAdapter(this)
// What should happen when
// the Grid View is touched
gridView.setOnTouchListener { _, event ->
// A Toast is generated for every touch and
// event action is set as ACTION_MOVE
Toast.makeText(applicationContext, "Scrolling is Disabled", Toast.LENGTH_SHORT).show()
event.action == MotionEvent.ACTION_MOVE
}
}
}
Output: Run the application
You can see that we are unable to scroll the Grid View vertically.
Note: You can add below image in the resource
Image downloaded from the Internet
Now simply copy-paste it into the Drawables folder in the res folder. Name them and click OK.
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read