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
How to Disable RecyclerView Scrolling in Android?
RecyclerView is a view group used for displaying data from arrays and databases. RecyclerView basically is a list of items from the data. RecyclerView is often referred to as a successor of GridView and ListView. More about RecyclerView could be found at RecyclerView in Android with Example. Recycle
3 min read
How to Detect End of ScrollView in Android?
In Android, a ScrollView is a view that lets the user scroll up and down to visit elements declared inside it. ScrollView is most commonly used to display TextView that contains a large amount of text which does not fit on a single instance of a screen. Users can scroll down and up to read complete
3 min read
Scroll ImageView in Android
In this article, we are going to implement a very important feature related to the ImageView. The image will keep on scrolling horizontally by itself. When we click on the image then it will stop scrolling and when we again click it will again keep scrolling. We can use this feature to show animatio
3 min read
Android GridView in Kotlin
A Grid View is a type of adapter view that is used to display the data in the grid layout format. For setting the data to the grid view adapter is used with the help of the setAdapter() method. This adapter helps to set the data from the database or array list to the items of the grid view. In this
6 min read
GridView in Android with Example
A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView. The main function of th
5 min read
GridView Using BaseAdapter in Android with Example
Here, we are designing an Android app to demonstrate the use of GridView layout. The GridView layout is a ViewGroup that groups view in a two-dimensional scrolling grid of rows and columns. Items in the grid view are inserted using a ListAdapter. The layout by default is scrollable and we don't need
6 min read
How to Implement GridView Inside a Fragment in Android?
A GridView is a versatile and efficient widget that allows you to display items in a grid-like fashion, perfect for presenting images, icons, or any structured data. Fragments, on the other hand, are essential components for building flexible and modular user interfaces. They enable the creation of
5 min read
Staggered GridView in Android with Example
StaggeredGridLayout is a LayoutManager in the Android studio similar to GridView, but in StaggeredGridLayout each grid has its own height and width.Difference Between GridView and Staggered GridViewStaggeredGridlayoutThe children are in a staggered grid format.It supports horizontal and vertical lay
4 min read
How to Enable/Disable Button in Android?
The Enable/Disable Feature is used in many Android apps so the basic use of that feature is to prevent the user from clicking on a button until they will add all the required fields that have been asked. We are considering an example of email and password if the user has entered the email and passwo
3 min read
How to Enable/Disable Click Listener on Views in Android?
Sometimes, when we come across a situation where we need not want to click on some clickable views. Like if mandatory data is not filled in by the user, So in that situation it is very helpful to have a clicks control over our views. So in this article, we will learn how to disable/enable click list
2 min read