Step1:- We have a RecyclerView in MainActivity
MainAcitivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginBottom="50dp"
android:layout_height="match_parent" >
<include
layout="@layout/toolbar"
>
</include>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/rec1"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="@+id/main_container"
>
</FrameLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="start"
android:layout_height="match_parent">
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:id="@+id/nav_view"
android:layout_height="match_parent"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"
/>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Step2:- We have to create a layout for recycler view items
order_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/productimage"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint"
android:layout_width="match_parent"
android:layout_height="122dp"
android:paddingStart="5dp"
android:paddingBottom="3dp">
<TextView
android:id="@+id/product_title"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:singleLine="false"
android:text="Le Berna"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/product_desc"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="1dp"
android:text="Testing"
android:textSize="9sp"
app:layout_constraintBottom_toTopOf="@+id/ratingBar4"
app:layout_constraintStart_toStartOf="@+id/product_price"
app:layout_constraintTop_toBottomOf="@+id/product_price" />
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:drawablePadding="1dp"
android:text="200"
android:textColor="#E91E63"
android:textSize="12dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/product_title" />
<TextView
android:id="@+id/distance_btn"
android:layout_width="wrap_content"
android:paddingTop="2dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="2dp"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:text="1.2km"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/product_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:gravity="left"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/product_title">
<RatingBar
android:id="@+id/ratingBar4"
style="@android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginTop="32dp"
android:foregroundGravity="left"
android:numStars="5"
android:rating="4.5"
android:scaleX=".4"
android:scaleY=".4"
android:stepSize="0.5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#16000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout4" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step3:- Create Model Class
ProductModel.java
package com.example.firebasedatabase.models;
public class ProductModel {
private int id;
private String catergoryname;
private String productimage;
private String desc;
private String distance;
public ProductModel(int id, String catergoryname, String productimage, String desc, String distance) {
this.id = id;
this.catergoryname = catergoryname;
this.productimage = productimage;
this.desc = desc;
this.distance = distance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCatergoryname() {
return catergoryname;
}
public void setCatergoryname(String catergoryname) {
this.catergoryname = catergoryname;
}
public String getProductimage() {
return productimage;
}
public void setProductimage(String productimage) {
this.productimage = productimage;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
}
Step4:- Now Create Adapter Class.
ProductAdapter:
package com.example.firebasedatabase.Adapters;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.firebasedatabase.R;
import com.example.firebasedatabase.models.ProductModel;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.holder> {
Context context;
List<ProductModel> productModels;
public ProductAdapter(Context context, List<ProductModel> productModels) {
this.context = context;
this.productModels = productModels;
}
@NonNull
@Override
public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.order_list_items,parent,false);
return new holder(view);
}
@Override
public void onBindViewHolder(@NonNull holder holder, int position) {
ProductModel model = productModels.get(position);
holder.product_title.setText(model.getCatergoryname());
holder.product_desc.setText(model.getDesc());
holder.distance_btn.setText(model.getDistance());
Glide.with(context)
.load(model.getProductimage())
.into(holder.productimage);
}
@Override
public int getItemCount() {
return productModels.size();
}
public class holder extends RecyclerView.ViewHolder {
ImageView productimage;
TextView product_title,product_desc,distance_btn;
public holder(@NonNull View itemView) {
super(itemView);
productimage=itemView.findViewById(R.id.productimage);
product_title=itemView.findViewById(R.id.product_title);
product_desc=itemView.findViewById(R.id.product_desc);
distance_btn=itemView.findViewById(R.id.distance_btn);
}
}
}
Step5:- MainActivity.java
package com.example.firebasedatabase;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import com.example.firebasedatabase.Adapters.ProductAdapter;
import com.example.firebasedatabase.models.ProductModel;
import com.google.android.material.navigation.NavigationView;
import java.util.ArrayList;
import java.util.List;
import java.util.function.ToLongBiFunction;
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
NavigationView nav;
RecyclerView recyclerView;
List<ProductModel> productModels = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
recyclerView= findViewById(R.id.rec1);
drawerLayout = findViewById(R.id.drawer_layout);
toolbar= findViewById(R.id.toolbar);
nav= findViewById(R.id.nav_view);
toggle= new ActionBarDrawerToggle(MainActivity.this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
fetchrecord();
}
public void fetchrecord(){
productModels.add(new ProductModel(1,"amit kumar","https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510__480.jpg","very food","2.5"));
productModels.add(new ProductModel(2,"amit kumar","https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510__480.jpg","very food","2.5"));
productModels.add(new ProductModel(3,"amit kumar","https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510__480.jpg","very food","2.5"));
ProductAdapter productAdapter = new ProductAdapter(this,productModels);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(productAdapter);
}
}
Now Recycler view part is completed . if you want to add drawer in this we have some more steps
Step1:- Toolbar
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:title="Firebase tutorial"
android:id="@+id/toolbar"
android:layout_height="60dp">
</androidx.appcompat.widget.Toolbar>
Step2:- nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
android:padding="10dp"
android:id="@+id/nav_header"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/userimage"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp"
app:layout_constraintEnd_toStartOf="@+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ashish Kumar" />
<TextView
android:id="@+id/textView5x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ashish@gmail.com" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Step3:- nav_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home" android:icon="@drawable/mainmenu"
android:title="Home"/>
<item android:id="@+id/nav_profile" android:title="Profile"
android:icon="@drawable/mainmenu"/>
<item android:id="@+id/nav_logout" android:icon="@drawable/mainmenu"
android:title="Logout"/>
</menu>
Dependency used:
Glide for image , and CircleImageView
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.github.bumptech.glide:glide:4.13.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'