0% found this document useful (0 votes)
24 views4 pages

Practical 5 A

The document outlines the creation of an Android application using Kotlin that displays a grid view of six animal images. It includes the implementation of a RecyclerView with an adapter that shows animal names and images, and displays a Toast message with the animal's name when an image is clicked. The application is successfully completed as per the practical requirements.

Uploaded by

shaikhtas15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Practical 5 A

The document outlines the creation of an Android application using Kotlin that displays a grid view of six animal images. It includes the implementation of a RecyclerView with an adapter that shows animal names and images, and displays a Toast message with the animal's name when an image is clicked. The application is successfully completed as per the practical requirements.

Uploaded by

shaikhtas15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 5a

Aim : To Create an android application kotlin using a grid view layout and insert 6 images of
different animals as the items. Toast the animal name when an image is clicked. (Recycler
View)

Animal.kt package
com.example.imagedashboard

data class
Animal( val name:
String, val
imageResId: Int )

AnimalAdapter.kt package
com.example.imagedashboard

import android.content.Context import


android.view.LayoutInflater import
android.view.View import
android.view.ViewGroup import
android.widget.ImageView import
android.widget.TextView import
android.widget.Toast import
androidx.recyclerview.widget.RecyclerView

class AnimalAdapter( private val


context: Context, private val
animalList: List<Animal>
) : RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder>() {

// ViewHolder to hold the image and text inner class


AnimalViewHolder(view: View) : RecyclerView.ViewHolder(view) { val
animalImage: ImageView = view.findViewById(R.id.animalImage) val
animalName: TextView = view.findViewById(R.id.animalName)

init {
// Set click listener to display the animal's name in a Toast
view.setOnClickListener { val position =
adapterPosition val animal = animalList[position]
Toast.makeText(context, animal.name, Toast.LENGTH_SHORT).show()
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnimalViewHolder {


val view = LayoutInflater.from(context).inflate(R.layout.item_animal, parent, false) return
AnimalViewHolder(view)
}

override fun onBindViewHolder(holder: AnimalViewHolder, position: Int) {


val animal = animalList[position]
holder.animalImage.setImageResource(animal.imageResId)
holder.animalName.text = animal.name
}

override fun getItemCount(): Int


{ return animalList.size
}
}

item_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical"
android:gravity="center" android:padding="16dp">

<!-- ImageView for displaying animal images -->


<ImageView
android:id="@+id/animalImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:layout_marginBottom="8dp"/>

<!-- TextView for animal name -->


<TextView
android:id="@+id/animalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animal Name"
android:gravity="center"
android:textSize="14sp" />
</LinearLayout>

MainActivity.kt
package com.example.imagedashboard
import android.os.Bundle import
androidx.appcompat.app.AppCompatActivity import
androidx.recyclerview.widget.GridLayoutManager import
androidx.recyclerview.widget.RecyclerView class
MainActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView


private lateinit var animalAdapter: AnimalAdapter

override fun onCreate(savedInstanceState: Bundle?)


{ super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize the RecyclerView


recyclerView = findViewById(R.id.recyclerView)

// Create a list of animals


val animals = listOf(
Animal("crock", R.drawable.crock),
Animal("fox", R.drawable.fox),
Animal("turtle", R.drawable.turtle),
Animal("Elephant", R.drawable.elephant),
Animal("Tiger", R.drawable.tiger),
Animal("snake", R.drawable.sanke)
)

// Set up the RecyclerView with a GridLayoutManager and the custom adapter


recyclerView.layoutManager = GridLayoutManager(this, 2) // 2 columns in the grid
animalAdapter = AnimalAdapter(this, animals)
recyclerView.adapter = animalAdapter
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"/> Conclusion :

Practical done successfully

You might also like