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

Android Animal Image Grid App

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)
252 views4 pages

Android Animal Image Grid App

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

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)

[Link] package
[Link]

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

[Link] package
[Link]

import [Link] import


[Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link] import
[Link]

class AnimalAdapter( private val


context: Context, private val
animalList: List<Animal>
) : [Link]<[Link]>() {

// ViewHolder to hold the image and text inner class


AnimalViewHolder(view: View) : [Link](view) { val
animalImage: ImageView = [Link]([Link]) val
animalName: TextView = [Link]([Link])

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

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


val view = [Link](context).inflate([Link].item_animal, parent, false) return
AnimalViewHolder(view)
}

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


val animal = animalList[position]
[Link]([Link])
[Link] = [Link]
}

override fun getItemCount(): Int


{ return [Link]
}
}

item_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
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>

[Link]
package [Link]
import [Link] import
[Link] import
[Link] import
[Link] class
MainActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView


private lateinit var animalAdapter: AnimalAdapter

override fun onCreate(savedInstanceState: Bundle?)


{ [Link](savedInstanceState)
setContentView([Link].activity_main)

// Initialize the RecyclerView


recyclerView = findViewById([Link])

// Create a list of animals


val animals = listOf(
Animal("crock", [Link]),
Animal("fox", [Link]),
Animal("turtle", [Link]),
Animal("Elephant", [Link]),
Animal("Tiger", [Link]),
Animal("snake", [Link])
)

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


[Link] = GridLayoutManager(this, 2) // 2 columns in the grid
animalAdapter = AnimalAdapter(this, animals)
[Link] = animalAdapter
}
}

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

<[Link]
xmlns:android="[Link]

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"/> Conclusion :

Practical done successfully

You might also like