Advertisement

Responsive Advertisement

Custom Calender By using naishadhparmar.zcustomcalendar

  • Implement in Gradle Module Then Sync
implementation 'org.naishadhparmar.zcustomcalendar:zcustomcalendar:1.0.1'
  • Implement below code in settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter()
}
}
rootProject.name = "Your Project Name here"
include ':app'
  • XML FILE OF DIALOG View

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="400dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#025C54">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day Detail"
android:textColor="@color/white"
android:textSize="20dp"
android:layout_marginLeft="20dp"/>
<ImageView
android:id="@+id/dialogcancel"
android:layout_width="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="10dp"
android:src="@drawable/ic_baseline_clear_24"
android:layout_height="match_parent">

</ImageView>
</androidx.appcompat.widget.Toolbar>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Date :"
android:textSize="15dp" />

<TextView
android:id="@+id/datedial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="@color/white"
android:gravity="center"
android:background="@drawable/notedetailsfield"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Event :"
android:textSize="15dp" />

<TextView
android:id="@+id/eventdial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="@color/white"
android:gravity="center"
android:background="@drawable/notedetailsfield"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Title :"
android:textSize="15dp" />

<TextView
android:id="@+id/titledial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:gravity="center"
android:layout_weight="0.5"
android:background="@drawable/notedetailsfield"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Description :"
android:textSize="15dp" />

<TextView
android:id="@+id/descriptiondial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:gravity="center"
android:layout_weight="0.5"
android:background="@drawable/notedetailsfield"/>

</LinearLayout>



</LinearLayout>








</androidx.cardview.widget.CardView>

  • Activity Main File

<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#174E49">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_arrow_back_24">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="@string/attendence_calender"
android:textColor="@color/white"
android:textSize="20sp">
</TextView>
<ImageView
android:id="@+id/sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="20dp"
android:src="@drawable/ic_baseline_sync_24">
</ImageView>
<ImageView
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="20dp"
android:src="@drawable/ic_baseline_format_list_bulleted_24">
</ImageView>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>

<org.naishadhparmar.zcustomcalendar.CustomCalendar
android:id="@+id/custom_calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:day_of_week_length="1"
app:day_of_week_start_from="sunday"
app:month_year_format="full_month__with_year"
app:row_height="50dp"
android:background="#CDDC39"
app:left_button_src="@drawable/ic_keyboard_arrow_left_black_24dp"
app:right_button_src="@drawable/ic_keyboard_arrow_right_black_24dp"
/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="25dp"
android:src="@drawable/present"
android:layout_height="25dp">

</ImageView>
<TextView
android:layout_width="wrap_content"
android:text="Present"
android:layout_height="match_parent">

</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="25dp"
android:src="@drawable/absent"
android:layout_height="25dp">

</ImageView>
<TextView
android:layout_width="wrap_content"
android:text="Absent"
android:layout_height="match_parent">

</TextView>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:src="@drawable/holiday"
android:layout_height="25dp">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:text="Holiday"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="3"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="25dp"
android:src="@drawable/approvalleave"
android:layout_height="25dp">

</ImageView>
<TextView
android:layout_width="wrap_content"
android:text="Approval Leave"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:src="@drawable/holiday"
android:layout_height="25dp">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:text="Weekly Off"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content">

</ProgressBar>
</LinearLayout>



  • Java File

package com.android.attendence;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.TextView;


import com.android.attendence.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;

import org.naishadhparmar.zcustomcalendar.CustomCalendar;
import org.naishadhparmar.zcustomcalendar.OnDateSelectedListener;
import org.naishadhparmar.zcustomcalendar.OnNavigationButtonClickedListener;
import org.naishadhparmar.zcustomcalendar.Property;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements OnNavigationButtonClickedListener {

ActivityMainBinding binding;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding=ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
syncronise();
binding.menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, Attendence.class);
startActivity(intent);
}
});


final CustomCalendar customCalendar = (CustomCalendar) findViewById(R.id.custom_calendar);


HashMap<Object, Property> mapDescToProp = new HashMap<>();

Property propPresent = new Property();
propPresent.layoutResource = R.layout.present;
propPresent.dateTextViewResource = R.id.present_datetextview;
mapDescToProp.put("Present", propPresent);

Property propAbsent = new Property();
propAbsent .layoutResource = R.layout.absent;
propAbsent.dateTextViewResource = R.id.absent_datetextview;
mapDescToProp.put("Absent", propAbsent );

Property propWeeklyoff = new Property();
propWeeklyoff.layoutResource = R.layout.weeklyoff;
propWeeklyoff.dateTextViewResource = R.id.weeklyoff_datetextview;
mapDescToProp.put("Weekly Off", propWeeklyoff);

Property propapprovleave = new Property();
propapprovleave.layoutResource = R.layout.approveleave;
propapprovleave.dateTextViewResource = R.id.approve_datetextview;
mapDescToProp.put("Approved Leave", propapprovleave);

Property propholiday = new Property();
propholiday.layoutResource = R.layout.holiday_view;
propholiday.dateTextViewResource = R.id.holiday_datetextview;
mapDescToProp.put("holiday",propholiday);

customCalendar.setMapDescToProp(mapDescToProp);

HashMap<Integer, Object> mapDateToDesc = new HashMap<>();
Calendar calendar = Calendar.getInstance();
mapDateToDesc.put(2, "Absent");
mapDateToDesc.put(5, "holiday");
mapDateToDesc.put(10, "Present"); //You don't need to explicitly mention "default" description dates.
mapDateToDesc.put(11, "Absent");
mapDateToDesc.put(3, "holiday");
mapDateToDesc.put(24, "holiday");
mapDateToDesc.put(21, "Absent");
mapDateToDesc.put(12, "Present");
mapDateToDesc.put(9, "Present");
mapDateToDesc.put(25, "Absent");
mapDateToDesc.put(6, "Weekly Off");
mapDateToDesc.put(13, "Weekly Off");
mapDateToDesc.put(20, "Weekly Off");
mapDateToDesc.put(27, "Weekly Off");
mapDateToDesc.put(23, "Present");
mapDateToDesc.put(29, "Approved Leave");
mapDateToDesc.put(8, "Approved Leave");
mapDateToDesc.put(1, "Approved Leave");
mapDateToDesc.put(22, "Approved Leave");

customCalendar.setOnNavigationButtonClickedListener(CustomCalendar.PREVIOUS, this);
customCalendar.setOnNavigationButtonClickedListener(CustomCalendar.NEXT, this);
customCalendar.setDate(calendar, mapDateToDesc);
customCalendar.setOnDateSelectedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(View view, Calendar selectedDate, Object desc) {

Log.d("TAG", "onDateSelected: "+desc);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.calblankdialog, viewGroup, false);
TextView datedial = dialogView.findViewById(R.id.datedial);
TextView eventTv = dialogView.findViewById(R.id.eventdial);
if(desc!=null){
eventTv.setText(desc.toString());
}
DateFormat date = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = date.format(selectedDate.getTimeInMillis());
datedial.setText(formattedDate);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
ImageView cancel =dialogView.findViewById(R.id.dialogcancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}


});
}
private void syncronise() {
binding.sync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.progress.setVisibility(View.VISIBLE);
}
});
}
@Override
public Map<Integer, Object>[] onNavigationButtonClicked(int whichButton, Calendar newMonth) {
Map<Integer, Object>[] arr = new Map[10];
switch(newMonth.get(Calendar.MONTH)) {
case Calendar.JANUARY:
arr[0] = new HashMap<>(); //This is the map linking a date to its description
arr[0].put(3, "Weekly Off");
arr[0].put(6, "holiday");
arr[0].put(21, "Weekly Off");
arr[0].put(24, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[1] = null; //Optional: This is the map linking a date to its tag.
break;
case Calendar.FEBRUARY:
arr[0] = new HashMap<>();
arr[0].put(5, "holiday");
arr[0].put(10, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[0].put(19, "Weekly Off");
break;
case Calendar.MARCH:
arr[0] = new HashMap<>();
arr[0].put(5, "holiday");
arr[0].put(10, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[0].put(19, "Weekly Off");
break;

default:
arr[0] = new HashMap<>();
}
return arr;



}


}

  • Java File
package com.android.attendence;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.TextView;


import com.android.attendence.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;

import org.naishadhparmar.zcustomcalendar.CustomCalendar;
import org.naishadhparmar.zcustomcalendar.OnDateSelectedListener;
import org.naishadhparmar.zcustomcalendar.OnNavigationButtonClickedListener;
import org.naishadhparmar.zcustomcalendar.Property;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements OnNavigationButtonClickedListener {

ActivityMainBinding binding;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding=ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final CustomCalendar customCalendar = (CustomCalendar) findViewById(R.id.custom_calendar);


HashMap<Object, Property> mapDescToProp = new HashMap<>();

Property propPresent = new Property();
propPresent.layoutResource = R.layout.present;
propPresent.dateTextViewResource = R.id.present_datetextview;
mapDescToProp.put("Present", propPresent);

Property propAbsent = new Property();
propAbsent .layoutResource = R.layout.absent;
propAbsent.dateTextViewResource = R.id.absent_datetextview;
mapDescToProp.put("Absent", propAbsent );

Property propWeeklyoff = new Property();
propWeeklyoff.layoutResource = R.layout.weeklyoff;
propWeeklyoff.dateTextViewResource = R.id.weeklyoff_datetextview;
mapDescToProp.put("Weekly Off", propWeeklyoff);

Property propapprovleave = new Property();
propapprovleave.layoutResource = R.layout.approveleave;
propapprovleave.dateTextViewResource = R.id.approve_datetextview;
mapDescToProp.put("Approved Leave", propapprovleave);

Property propholiday = new Property();
propholiday.layoutResource = R.layout.holiday_view;
propholiday.dateTextViewResource = R.id.holiday_datetextview;
mapDescToProp.put("holiday",propholiday);

customCalendar.setMapDescToProp(mapDescToProp);

HashMap<Integer, Object> mapDateToDesc = new HashMap<>();
Calendar calendar = Calendar.getInstance();
mapDateToDesc.put(2, "Absent");
mapDateToDesc.put(5, "holiday");
mapDateToDesc.put(10, "Present"); //You don't need to explicitly mention "default" description dates.
mapDateToDesc.put(11, "Absent");
mapDateToDesc.put(3, "holiday");
mapDateToDesc.put(24, "holiday");
mapDateToDesc.put(21, "Absent");
mapDateToDesc.put(12, "Present");
mapDateToDesc.put(9, "Present");
mapDateToDesc.put(25, "Absent");
mapDateToDesc.put(6, "Weekly Off");
mapDateToDesc.put(13, "Weekly Off");
mapDateToDesc.put(20, "Weekly Off");
mapDateToDesc.put(27, "Weekly Off");
mapDateToDesc.put(23, "Present");
mapDateToDesc.put(29, "Approved Leave");
mapDateToDesc.put(8, "Approved Leave");
mapDateToDesc.put(1, "Approved Leave");
mapDateToDesc.put(22, "Approved Leave");

customCalendar.setOnNavigationButtonClickedListener(CustomCalendar.PREVIOUS, this);
customCalendar.setOnNavigationButtonClickedListener(CustomCalendar.NEXT, this);
customCalendar.setDate(calendar, mapDateToDesc);
customCalendar.setOnDateSelectedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(View view, Calendar selectedDate, Object desc) {

Log.d("TAG", "onDateSelected: "+desc);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.calblankdialog, viewGroup, false);
TextView datedial = dialogView.findViewById(R.id.datedial);
TextView eventTv = dialogView.findViewById(R.id.eventdial);
if(desc!=null){
eventTv.setText(desc.toString());
}
DateFormat date = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = date.format(selectedDate.getTimeInMillis());
datedial.setText(formattedDate);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
ImageView cancel =dialogView.findViewById(R.id.dialogcancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}


});
}

@Override
public Map<Integer, Object>[] onNavigationButtonClicked(int whichButton, Calendar newMonth) {
Map<Integer, Object>[] arr = new Map[10];
switch(newMonth.get(Calendar.MONTH)) {
case Calendar.JANUARY:
arr[0] = new HashMap<>(); //This is the map linking a date to its description
arr[0].put(3, "Weekly Off");
arr[0].put(6, "holiday");
arr[0].put(21, "Weekly Off");
arr[0].put(24, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[1] = null; //Optional: This is the map linking a date to its tag.
break;
case Calendar.FEBRUARY:
arr[0] = new HashMap<>();
arr[0].put(5, "holiday");
arr[0].put(10, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[0].put(19, "Weekly Off");
break;
case Calendar.MARCH:
arr[0] = new HashMap<>();
arr[0].put(5, "holiday");
arr[0].put(10, "holiday");
arr[0].put(29, "Approved Leave");
arr[0].put(8, "Approved Leave");
arr[0].put(1, "Approved Leave");
arr[0].put(22, "Approved Leave");
arr[0].put(19, "Weekly Off");
break;

default:
arr[0] = new HashMap<>();
}
return arr;



}


}
  • Paste below code if custom calender threw error 👇
android.enableJetifier = true
  • NOTE ;- I have used view binding for this project. So, you can use viewbinding or continue with findviewbyid.

Post a Comment

0 Comments