- Firstly take in main xml file recyclerview and id and mentioned it into main java file.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
///WRITE INTO JAVA MAIN FILE recyclerview=findViewById(R.id.recycler_view);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
ab.back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Attendence.this,MainActivity.class);
startActivity(intent);
}
});
model.add(new AttendenceModel("Jun\n10","10:42:23 AM","01:30:23 PM"));
model.add(new AttendenceModel("July\n10","10:42:23 AM","01:30:23 PM"));
model.add(new AttendenceModel("Aug\n13","10:42:23 AM","01:30:23 PM"));
model.add(new AttendenceModel("Jun\n10","10:42:23 AM","01:30:23 PM"));
model.add(new AttendenceModel("Jun\n10","10:42:23 AM","01:30:23 PM"));
model.add(new AttendenceModel("Jun\n10","10:42:23 AM","01:30:23 PM"));
AttendenceRecycler Adapter=new AttendenceRecycler(this,model);
recyclerview.setAdapter(Adapter);///Here model.add comes in the last process when we'll create model and adapter
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="7dp"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/date"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:text="date"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="14dp">
</TextView>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#C8C7C7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:layout_marginHorizontal="10dp"
android:text="@string/checkin_checkout"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/timing_from"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/teal_700"
android:text="From Time"
android:textSize="14sp"
>
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/teal_700"
android:text=" -> "
android:layout_gravity="bottom"
android:textSize="14sp"
>
</TextView>
<TextView
android:id="@+id/timing_to"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/teal_700"
android:text="Till Time"
android:textSize="14sp"
>
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/teal_700"
android:text="P"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="@color/white">
</androidx.appcompat.widget.AppCompatButton>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
- then create model and initialize of format created design.as example 👇
package com.android.attendence;
public class AttendenceModel {
String Date,DateFrom,DateTo;
public AttendenceModel(String Date,String DateFrom, String DateTo){
this.Date=Date;
this.DateFrom=DateFrom;
this.DateTo=DateTo;
}
}
- then create recyclerview adapter
package com.android.attendence;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class AttendenceRecycler extends RecyclerView.Adapter<AttendenceRecycler.Viewholder> {
Context context;
ArrayList<AttendenceModel> arrmodel;
AttendenceRecycler(Context context, ArrayList<AttendenceModel> arrmodel){
this.context=context;
this.arrmodel=arrmodel;
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.attendence_row,parent,false);
Viewholder viewholder=new Viewholder(view);
return viewholder;
}
@Override
public void onBindViewHolder(@NonNull Viewholder holder, int position) {
holder.txtdate.setText(arrmodel.get(position).Date); ///biniding in the recycler view
holder.txtfromdate.setText(arrmodel.get(position).DateFrom);
holder.txttodate.setText(arrmodel.get(position).DateTo);
}
@Override
public int getItemCount() {
return arrmodel.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
TextView txtdate,txtfromdate,txttodate; //create textview for the recycler
public Viewholder(@NonNull View itemView) {
super(itemView);
txtdate=itemView.findViewById(R.id.date);
txtfromdate=itemView.findViewById(R.id.timing_from); //Here it will take format layout.
txttodate=itemView.findViewById(R.id.timing_to);
}
}
}
0 Comments