How to Implement RecylerView Inside a Dialogue Window?
Last Updated :
24 Apr, 2023
RecyclerView is a ViewGroup added to the Android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible the construction of any lists with XML layouts as an item that can be customized vastly while improving on the efficiency of ListViews and GridViews. In this article, we are going to see how can we implement a RecylerView inside a Dialogue Window. For this we have to follow some steps:
To implement a RecyclerView inside a Dialog, you can follow these steps are as follows:
- Create a layout file for the Dialog and add a RecyclerView to it.
- Create a class for the Dialog and set its content view to the layout file you created.
- Create an adapter for the RecyclerView and pass it to Dialog’s setAdapter method.
Step by Step Implementation:
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: Creating a layout for RecyclerView
Here we have to create a layout for our RecyclerView to display our items, Navigate to app > res > layout > Create a new layout file named item_employee.xml, it contains two textViews for displaying the name and email of the employee.
item_employee.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:id = "@+id/llMain"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:gravity = "center_vertical"
android:orientation = "horizontal"
android:padding = "10dp" >
< androidx.cardview.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginStart = "8dp"
android:layout_marginEnd = "8dp"
android:background = "@color/white"
android:foreground = "?attr/selectableItemBackground"
app:cardCornerRadius = "3dp"
app:cardElevation = "3dp"
app:cardUseCompatPadding = "true" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:padding = "15dp"
android:orientation = "vertical" >
< TextView
android:id = "@+id/tvName"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textColor = "@android:color/black"
android:textSize = "20sp"
tools:text = "Name" />
< TextView
android:id = "@+id/tvEmail"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:textColor = "#454545"
android:textSize = "16sp"
tools:text = "Email" />
</ LinearLayout >
</ androidx.cardview.widget.CardView >
</ LinearLayout >
|
Step 4: Creating a Dialog Window Layout File
Navigate to app > res > layout > create a new layout file named Dialog_list.xml, it contains the RecylerView and displays a list of the employee inside a dialog window.
Dialog_list.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:orientation = "vertical"
android:layout_height = "match_parent" >
< androidx.cardview.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
app:cardCornerRadius = "10dp"
app:cardElevation = "10dp"
app:cardUseCompatPadding = "true" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
android:padding = "10dp" >
< TextView
android:id = "@+id/tvTitle"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:padding = "10dp"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold"
android:text = "List of Employees"
android:visibility = "visible" />
< View
android:layout_width = "match_parent"
android:layout_height = "2dp"
android:layout_marginTop = "10dp"
android:layout_marginBottom = "10dp"
android:background = "#308d46" />
< androidx.recyclerview.widget.RecyclerView
android:id = "@+id/rvList"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:visibility = "visible"
tools:listitem = "@layout/item_employee" />
</ LinearLayout >
</ androidx.cardview.widget.CardView >
</ LinearLayout >
|
Step 5: Working with activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to the activity_main.xml file. Below is the code for the activity_main.xml file. The activity_main.xml represents the UI part of our application, it contains a TextView and a RecylerView to display the list of items.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:orientation = "vertical"
android:layout_height = "match_parent" >
< androidx.cardview.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
app:cardCornerRadius = "10dp"
app:cardElevation = "10dp"
app:cardUseCompatPadding = "true" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
android:padding = "10dp" >
< TextView
android:id = "@+id/tvTitle"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:padding = "10dp"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold"
android:text = "List of Employees"
android:visibility = "visible" />
< View
android:layout_width = "match_parent"
android:layout_height = "2dp"
android:layout_marginTop = "10dp"
android:layout_marginBottom = "10dp"
android:background = "#308d46" />
< androidx.recyclerview.widget.RecyclerView
android:id = "@+id/rvList"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:visibility = "visible"
tools:listitem = "@layout/item_employee" />
</ LinearLayout >
</ androidx.cardview.widget.CardView >
</ LinearLayout >
|
Step 6: Creating an Employee Model
In this step, we are going to create an employee model for our RecyclerView. It Contains the Employee’s Name and Employee email Id, and it was going to be a Serializable model because we are going to pass the object of this Employee model to another Activity. Below is the code for the Employee model. Navigate to app >java > your package name > Create a Kotlin data class named it Employee.kt.
Employee.kt:
Kotlin
package com.example.geeksforgeeks
import android.os.Parcel
import android.os.Parcelable
data class Employee(
val name:String,
val email:String
):java.io.Serializable
|
Java
package com.example.geeksforgeeks;
import java.io.Serializable;
public class Employee implements Serializable {
private String name;
private String email;
public Employee(String name, String email) {
this .name = name;
this .email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
|
Step 7: Creating Employee ArrayList
In this step, we are going to prepare the List of employees with the employee name and employee email. Below is the code for Creating and adding data to the ArrayList. Comments are added inside the code for a better understanding of the Code. Navigate to app > java >your package name > Create a Kotlin Object named Constants.
Constants.kt:
Kotlin
package com.example.geeksforgeeks
object Constants {
fun getEmployeeData():ArrayList<Employee>{
val employeeList=ArrayList<Employee>()
val emp1=Employee( "Chinmaya Mohapatra" , "chinmaya@gmail.com" )
employeeList.add(emp1)
val emp2=Employee( "Ram prakash" , "ramp@gmail.com" )
employeeList.add(emp2)
val emp3=Employee( "OMM Meheta" , "mehetaom@gmail.com" )
employeeList.add(emp3)
val emp4=Employee( "Hari Mohapatra" , "harim@gmail.com" )
employeeList.add(emp4)
val emp5=Employee( "Abhisek Mishra" , "mishraabhi@gmail.com" )
employeeList.add(emp5)
val emp6=Employee( "Sindhu Malhotra" , "sindhu@gmail.com" )
employeeList.add(emp6)
val emp7=Employee( "Anil sidhu" , "sidhuanil@gmail.com" )
employeeList.add(emp7)
val emp8=Employee( "Sachin sinha" , "sinhas@gmail.com" )
employeeList.add(emp8)
val emp9=Employee( "Amit sahoo" , "sahooamit@gmail.com" )
employeeList.add(emp9)
val emp10=Employee( "Raj kumar" , "kumarraj@gmail.com" )
employeeList.add(emp10)
return employeeList
}
}
|
Java
package com.example.geeksforgeeks;
import java.util.ArrayList;
public class Constants {
public static ArrayList<Employee> getEmployeeData() {
ArrayList<Employee> employeeList = new ArrayList<>();
Employee emp1 = new Employee( "Chinmaya Mohapatra" , "chinmaya@gmail.com" );
employeeList.add(emp1);
Employee emp2 = new Employee( "Ram prakash" , "ramp@gmail.com" );
employeeList.add(emp2);
Employee emp3 = new Employee( "OMM Meheta" , "mehetaom@gmail.com" );
employeeList.add(emp3);
Employee emp4 = new Employee( "Hari Mohapatra" , "harim@gmail.com" );
employeeList.add(emp4);
Employee emp5 = new Employee( "Abhisek Mishra" , "mishraabhi@gmail.com" );
employeeList.add(emp5);
Employee emp6 = new Employee( "Sindhu Malhotra" , "sindhu@gmail.com" );
employeeList.add(emp6);
Employee emp7 = new Employee( "Anil sidhu" , "sidhuanil@gmail.com" );
employeeList.add(emp7);
Employee emp8 = new Employee( "Sachin sinha" , "sinhas@gmail.com" );
employeeList.add(emp8);
Employee emp9 = new Employee( "Amit sahoo" , "sahooamit@gmail.com" );
employeeList.add(emp9);
Employee emp10 = new Employee( "Raj kumar" , "kumarraj@gmail.com" );
employeeList.add(emp10);
return employeeList;
}
}
|
Step 8: Create an Adapter for our RecylerView
As we know for every RecylerView we need an Adapter class. and Implement the Three member functions.
- onCreateViewHolder: Here we have to connect the layout resource file that we are for our RecylerView.
- onBindViewHolder: Here we have to bind our items to the data source.
- getItemCount: Here we have to return the length of our ArrayList.
Here we are going to Apply OnClickListener to our RecylerView Adapter by Implementing OnClickListener Interface. Navigate to app > java >your package name > Create a Kotlin Class named as EmployeeAdapter. Below is the code for the ItemAdapter class. Comments are added inside the code for a better understanding of the Code.
EmployeeAdapter.kt:
Kotlin
package com.example.geeksforgeeks
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList
class EmployeeAdapter(
private val context: Context,
private var list: ArrayList<Employee>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_employee, parent, false )
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = list[position]
if (holder is MyViewHolder) {
holder.name.text = item.name
holder.email.text = item.email
}
}
override fun getItemCount(): Int {
return list.size
}
private class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val name = view.findViewById<TextView>(R.id.tvName)
val email = view.findViewById<TextView>(R.id.tvEmail)
}
}
|
Java
package com.example.geeksforgeeks;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class EmployeeAdapter
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<Employee> list;
public EmployeeAdapter(Context context,
ArrayList<Employee> list)
{
this .context = context;
this .list = list;
}
@Override
public RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(context).inflate(
R.layout.item_employee, parent, false );
return new MyViewHolder(view);
}
@Override
public void
onBindViewHolder(RecyclerView.ViewHolder holder,
int position)
{
Employee item = list.get(position);
if (holder instanceof MyViewHolder) {
((MyViewHolder)holder)
.name.setText(item.getName());
((MyViewHolder)holder)
.email.setText(item.getEmail());
}
}
@Override public int getItemCount()
{
return list.size();
}
private static class MyViewHolder
extends RecyclerView.ViewHolder {
TextView name;
TextView email;
MyViewHolder(View view)
{
super (view);
name = view.findViewById(R.id.tvName);
email = view.findViewById(R.id.tvEmail);
}
}
}
|
Step 9: Creating a Class for Dialog Window
In this step we are going to create a Kotlin class to set out the dialog window, Navigate to app > java >your package name > Create a Kotlin Class named as DialogList, below is the code for DialogList.kt.
DialogList.kt:
Kotlin
package com.example.geeksforgeeks
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
abstract class DialogList(
context: Context,
private var list: ArrayList<Employee>,
) : Dialog(context) {
private var adapter: EmployeeAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState ?: Bundle())
val view = LayoutInflater.from(context).inflate(R.layout.dialog_list, null )
setContentView(view)
setCanceledOnTouchOutside( true )
setCancelable( true )
setUpRecyclerView(view)
}
private fun setUpRecyclerView(view: View) {
view.findViewById<RecyclerView>(R.id.rvList).layoutManager = LinearLayoutManager(context)
adapter = EmployeeAdapter(context, list)
view.findViewById<RecyclerView>(R.id.rvList).adapter = adapter
}
}
|
Java
package com.example.geeksforgeeks;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public abstract class DialogList extends Dialog {
private ArrayList<Employee> list;
private EmployeeAdapter adapter;
public DialogList(Context context,
ArrayList<Employee> list)
{
super (context);
this .list = list;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState != null
? savedInstanceState
: new Bundle());
View view
= LayoutInflater.from(getContext())
.inflate(R.layout.dialog_list, null );
setContentView(view);
setCanceledOnTouchOutside( true );
setCancelable( true );
setUpRecyclerView(view);
}
private void setUpRecyclerView(View view)
{
RecyclerView recyclerView
= view.findViewById(R.id.rvList);
recyclerView.setLayoutManager(
new LinearLayoutManager(getContext()));
adapter = new EmployeeAdapter(getContext(), list);
recyclerView.setAdapter(adapter);
}
}
|
Step 10: Working with MainActivity File
In this step, we are going to Get the employeelist by calling the Constants getEmployeeData() method and pass the employeelist to the DialogList class and display the employee list. Comments are added inside the code for a better understanding of the Code.
MainActivity.kt:
Kotlin
package com.example.geeksforgeeks
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_open=findViewById<Button>(R.id.open)
btn_open.setOnClickListener {
EmployeeListDialough()
}
}
fun EmployeeListDialough() {
val employeeList: ArrayList<Employee> = Constants.getEmployeeData()
val listDialog = object : DialogList(
this @MainActivity ,
employeeList
) {
}
listDialog.show()
}
}
|
Java
package com.example.geeksforgeeks;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnOpen = findViewById(R.id.open);
btnOpen.setOnClickListener(
new View.OnClickListener() {
@Override public void onClick(View v)
{
EmployeeListDialough();
}
});
}
public void EmployeeListDialough()
{
ArrayList<Employee> employeeList
= Constants.getEmployeeData();
DialogList listDialog = new DialogList(
this , employeeList) {
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
}
};
listDialog.show();
}
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...