Kotlin GridView Adapter Tutorial -
Kotlin GridView Adapter Tutorial
by on on android

Kotlin GridView Adapter Tutorial

HOW TO USE KOTLIN GRIDVIEW ADAPTER?

How to use Kotlin GridView Adapter is very easy. GridView Adapter helps us to list out array items using a custom view resource file.

Image that those stats were coming from an ArrayList, You will find it hard to do it with just normal design technique. 

Step by Step Guide to use Kotlin GridView Adapter.

1. Go to C:\Users\sam\Desktop\Apps\kotlin\ApptitudeTest\app\src\main\res\layout and create a layout resource file called item List. Inside that file add this lines of code to it.

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff">    <ImageView        android:id="@+id/stat_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.545"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:srcCompat="@drawable/icon_15_trophy" />    <TextView        android:id="@+id/stat_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="16.7dp"        android:fontFamily="@font/opensansbold"        android:text="Total Score"        android:textColor="#1CC4B8"        app:layout_constraintEnd_toEndOf="@+id/stat_image"        app:layout_constraintStart_toStartOf="@+id/stat_image"        app:layout_constraintTop_toBottomOf="@+id/stat_image" /></androidx.constraintlayout.widget.ConstraintLayout>

2. Create your Kotlin Array List inside your MainActivity class. Call the Custom Adapter and pass the ArrayList. 

var allStat: ArrayList<StatFeed> = ArrayList();            allStat.add(StatFeed("Total score", "k"))            allStat.add(StatFeed("Total Test", "k"))            allStat.add(StatFeed("Previous Score", "k"))            allStat.add(StatFeed("Time Taken", "k"))            val simpleGrid: GridView = findViewById<GridView>(R.id.simpleGridView);            simpleGrid.adapter = CustomAdapter(this, allStat);


3. Create your Custom Adapter Class. Add these lines of code to it.

class CustomAdapter(    private val context: Context,    private val AllStats: ArrayList<StatFeed>) :    BaseAdapter() {    override fun getCount(): Int {        return AllStats.count();    }    override fun getItem(position: Int): Any {        return position.toLong();    }    override fun getItemId(position: Int): Long {        return position.toLong()    }    override fun getView(position: Int, view: View?, viewGroup: ViewGroup?): View {        val layoutInflater: LayoutInflater = LayoutInflater.from(context);        val mainRow: View = layoutInflater.inflate(R.layout.itemlist, viewGroup, false);        val answerText: TextView = mainRow.findViewById<TextView>(R.id.stat_text);        answerText.text = AllStats[position].name;        return mainRow;    }}


Finally run your application. Everything will work perfectly.