Lab Detail


Sno Back Back Subject subject date title note
1 1 Back to subject Mobile Application Development Lab - 20A05706 (Lab) Sept. 14, 2025 RECYCLERVIEW

RECYCLERVIEW

RecyclerView is a powerful and flexible UI component introduced in Android's support library to efficiently display large datasets in a scrollable list or grid format. It's an improved version of the older ListView and GridView components, providing better performance, more flexibility, and easier customization. Here's an explanation of RecyclerView in Android:

• Purpose:

• RecyclerView is designed to efficiently display large datasets in a scrollable list or grid format.

• It efficiently manages the memory usage by recycling views as they move off-screen, reducing the need for creating and destroying views dynamically.

• RecyclerView provides a flexible architecture for implementing various layout managers, item decorations, and item animations.

 

E:\android\MyApplication\app\build.gradle.kts

android {
    dataBinding{
        enable=true
        //enabled=true old version of android sdk Do it
    }

}

E:\android\MyApplication\app\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

E:\android\MyApplication\app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <RelativeLayout
        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"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Recycler view example"
            android:id="@+id/tv1"
            />
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rec"
            android:layout_below="@id/tv1"
            android:layout_margin="10dp"/>
    </RelativeLayout>

</layout>

E:\android\MyApplication\app\src\main\java\com\example\myapplication\MainActivity.kt

package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    var binding: ActivityMainBinding? = null

//    var Names=arrayOf("AAA","BBB","CCC")
//    var Mobile=arrayOf("5253","87567","7565")
//    var Emails=arrayOf("aaa@gmail.com","bbb@yamil.com","ccc@hotmail.com")
//    var Profile=arrayOf(R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher)
var Names = arrayOf(
    "AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH","III","JJJ",
    "KKK","LLL","MMM","NNN","OOO","PPP","QQQ","RRR","SSS","TTT"
)

    var Mobile = arrayOf(
        "5253","87567","7565","1234","9876","5432","6789","3456","7890","2345",
        "4567","6781","2346","9872","5643","6784","3457","9873","4568","7891"
    )

    var Emails = arrayOf(
        "aaa@gmail.com","bbb@yamil.com","ccc@hotmail.com","ddd@gmail.com","eee@yahoo.com",
        "fff@hotmail.com","ggg@gmail.com","hhh@yahoo.com","iii@hotmail.com","jjj@gmail.com",
        "kkk@yahoo.com","lll@hotmail.com","mmm@gmail.com","nnn@yahoo.com","ooo@hotmail.com",
        "ppp@gmail.com","qqq@yahoo.com","rrr@hotmail.com","sss@gmail.com","ttt@yahoo.com"
    )

    var Profile = arrayOf(
        R.mipmap.ic_launcher, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher_round, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher_round
    )


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Enable edge-to-edge content
        enableEdgeToEdge()
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        var linearlayoutmanager= LinearLayoutManager(this@MainActivity,LinearLayoutManager.VERTICAL, false)
        binding?.rec?.layoutManager=linearlayoutmanager
        var customAdapter=CustomAdapter(this@MainActivity, Names,Mobile, Emails, Profile)
        binding?.rec?.adapter=customAdapter

    }
}

E:\android\MyApplication\app\src\main\res\layout\custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        android:layout_margin="5dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linear"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:layout_margin="5dp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mobile"
            android:layout_margin="5dp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/email"
            android:layout_margin="5dp"/>
    </LinearLayout>
</LinearLayout>

E:\android\MyApplication\app\src\main\java\com\example\myapplication\CustomAdapter.kt

package com.example.myapplication
import android.widget.Toast
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CustomAdapter(mainActivity: MainActivity, names: Array<String>, mobile: Array<String>, emails: Array<String>, profile: Array<Int>) : RecyclerView.Adapter<CustomAdapter.MyViewHolder>() {
    var adpnames: Array<String>
    var adpmobile: Array<String>
    var adpemail: Array<String>
    var adpprofile: Array<Int>
    var adpContext: Context

    init {
        adpnames=names
        adpemail=emails
        adpmobile=mobile
        adpprofile=profile
        adpContext=mainActivity
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):CustomAdapter.MyViewHolder {
        var view=LayoutInflater.from(parent.context).inflate(R.layout.custom,parent,false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: CustomAdapter.MyViewHolder,position: Int) {
        holder.name?.text=adpnames[holder.adapterPosition]
        holder.email?.text=adpemail[holder.adapterPosition]
        holder.mobile?.text=adpmobile[holder.adapterPosition]
        holder.profile?.setImageResource(adpprofile[holder.adapterPosition]
        )

        // When the profile image is clicked
        holder?.profile?.setOnClickListener {
            val info = "Name: ${adpnames[position]}\n" +
                    "Email: ${adpemail[position]}\n" +
                    "Mobile: ${adpmobile[position]}\n" +
                    "Profile ID: ${adpprofile[position]}"
            Toast.makeText(adpContext, info, Toast.LENGTH_LONG).show()
        }

        // When the whole item is clicked, show all information
        holder?.linearLayout?.setOnClickListener {
            val info = "Name: ${adpnames[position]}\n" +
                    "Email: ${adpemail[position]}\n" +
                    "Mobile: ${adpmobile[position]}\n" +
                    "Profile ID: ${adpprofile[position]}"
            Toast.makeText(adpContext, info, Toast.LENGTH_LONG).show()
        }

    }
    override fun getItemCount(): Int {
        return adpnames.size
    }
    inner class MyViewHolder (itemview:
                              View):RecyclerView.ViewHolder(itemview) {
        var name: TextView? = null
        var mobile: TextView? = null
        var email: TextView? = null
        var profile: ImageView? = null
        var linearLayout: LinearLayout? = null

        init {
            name = itemview.findViewById(R.id.name)
            mobile = itemview.findViewById(R.id.mobile)
            email = itemview.findViewById(R.id.email)
            profile = itemview.findViewById(R.id.iv)
            linearLayout = itemview.findViewById(R.id.linear)
        }
    }

}

Output