Thu. Jan 9th, 2025

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rec"
android:layout_margin="10dp"/>

</LinearLayout>
</layout>

MainActivity.kt

package com.androindian.recycler

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.androindian.recycler.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
//1
var names=arrayOf("AAAA","BBBB","CCCC","DDD")
var numbers=arrayOf("223543","12324","343454","23354")
var images=arrayOf(R.mipmap.ic_launcher_round,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher_round,
R.mipmap.ic_launcher_round)
var emails=arrayOf("AAAA@gmail.com ","BBBB@gmail.com","CCCC@gmail.com","DDD@gmail.com")
var binding:ActivityMainBinding?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding= DataBindingUtil.setContentView(this,R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

//2
var linearLayoutManager=LinearLayoutManager(this@MainActivity,LinearLayoutManager.VERTICAL,true)
binding?.rec?.layoutManager=linearLayoutManager

//3
var customAdapter=CustomaAdapter(this@MainActivity,names,numbers,images,emails)
binding?.rec?.adapter=customAdapter
}
}

CustomaAdapter.kt

package com.androindian.recycler

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.androindian.recycler.databinding.CustomadapterBinding

class CustomaAdapter(mainActivity: MainActivity, names: Array<String>, numbers: Array<String>, images: Array<Int>, emails: Array<String>) :
RecyclerView.Adapter<CustomaAdapter.MyViewHolder>() {
//var binding:CustomBinding?=null
var adpnames: Array<String>
var adpimges: Array<Int>
var adpnumbers: Array<String>
var adpemial: Array<String>
var adpContext: Context
init {
adpemial = emails
adpnames = names
adpnumbers = numbers
adpimges = images
adpContext = mainActivity
}
//
class MyViewHolder(itemView: View) : ViewHolder(itemView) {
val binding: CustomadapterBinding = DataBindingUtil.getBinding(itemView)!!

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomaAdapter.MyViewHolder {

val binding: CustomadapterBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.customadapter,
null,
false
)

return binding?.root?.let { MyViewHolder(it) }!!
}

override fun onBindViewHolder(holder: CustomaAdapter.MyViewHolder, position: Int) {
holder.binding?.name?.text = adpnames.get(holder.adapterPosition)
holder.binding?.mobile?.text=adpnumbers.get(holder.adapterPosition)
holder.binding?.email?.text=adpemial.get(holder.adapterPosition)
holder?.binding?.img?.setImageResource(adpimges.get(holder.adapterPosition))
}

override fun getItemCount(): Int {
return adpemial.size
}

}

customadapter.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
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:layout_margin="5dp"
android:id="@+id/img"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:layout_margin="10dp"/>
</LinearLayout>



</LinearLayout>
</layout>

build.gradle

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
}

android {
namespace = "com.androindian.recycler"
compileSdk = 35

defaultConfig {
applicationId = "com.androindian.recycler"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dataBinding{
enable=true
}

}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *