How to Disable GridView Scrolling in Android?
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 w3wiki 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
Now simply copy-paste it into the Drawables folder in the res folder. Name them and click OK.