Skip to content

Commit 7360ebe

Browse files
Merge pull request #5 from shahzadafridi/klus-desing-cv
XML value configuration and disable date which not include in selecte…
2 parents c376b7e + 3ab3caf commit 7360ebe

File tree

10 files changed

+203
-105
lines changed

10 files changed

+203
-105
lines changed

CalendarView/src/main/java/com/shahzadafridi/calendarview/CalendarAdapter.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.shahzadafridi.calendarview
22

33
import android.content.Context
44
import android.graphics.Color
5+
import android.graphics.Typeface
56
import android.graphics.drawable.ShapeDrawable
67
import android.graphics.drawable.shapes.RoundRectShape
8+
import android.util.Log
79
import android.view.LayoutInflater
810
import android.view.View
911
import android.view.ViewGroup
@@ -12,6 +14,7 @@ import android.widget.TextView
1214
import androidx.core.content.ContextCompat
1315
import androidx.core.content.res.ResourcesCompat
1416
import androidx.recyclerview.widget.RecyclerView
17+
import java.lang.Exception
1518
import java.util.*
1619

1720

@@ -24,6 +27,7 @@ class CalendarAdapter(
2427
monthNumber: Int
2528
) : RecyclerView.Adapter<CalendarAdapter.MyViewHolder>() {
2629

30+
val TAG: String = "CalendarAdapter"
2731
// for view inflation
2832
private val inflater: LayoutInflater
2933
private val mContext: Context
@@ -33,7 +37,7 @@ class CalendarAdapter(
3337
private var day_font: Int? = null
3438
private var day_bg: Int? = null
3539
private var day_txt_clr: Int? = null
36-
private var day_txt_size: Int? = null
40+
private var day_txt_size: Float? = null
3741
private var day_selected_txt_clr: Int? = null
3842
private var day_selected_bg: Int? = null
3943
private var event_dot_clr: Int? = null
@@ -83,13 +87,17 @@ class CalendarAdapter(
8387
rowLayout.setOnClickListener(this)
8488
rowLayout.setOnLongClickListener(this)
8589
day_txt_size?.let {
86-
textView.textSize = it.toFloat()
90+
textView.textSize = it
8791
}
8892
day_txt_clr?.let {
8993
textView.setTextColor(ContextCompat.getColor(mContext, it))
9094
}
91-
day_font?.let {
92-
textView.typeface = ResourcesCompat.getFont(mContext, it)
95+
try {
96+
day_font?.let {
97+
textView.typeface = ResourcesCompat.getFont(mContext, it)
98+
}
99+
}catch (e: Exception){
100+
Log.e(TAG, "$day_font not found!")
93101
}
94102
day_bg?.let {
95103
rowLayout.setBackgroundResource(it)
@@ -115,6 +123,7 @@ class CalendarAdapter(
115123
if (month != mMonthNumber || year != today.get(Calendar.YEAR)) {
116124
// if this day is outside current month, grey it out
117125
holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.greyed_out))
126+
holder.rowLayout.isEnabled = false
118127
} else if (today.get(Calendar.DATE) == day && today.get(Calendar.MONTH) == month && today.get(Calendar.YEAR) == year) {
119128
// if it is today, set it to blue/bold
120129
day_selected_txt_clr?.let {
@@ -127,6 +136,7 @@ class CalendarAdapter(
127136
} ?:run {
128137
holder.rowLayout.setBackgroundResource(R.drawable.ic_black_oval)
129138
}
139+
holder.rowLayout.isEnabled = true
130140
}
131141

132142
textViewEvent.visibility = View.GONE

CalendarView/src/main/java/com/shahzadafridi/calendarview/CalendarView.kt

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,70 @@
11
package com.shahzadafridi.calendarview
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
5+
import android.graphics.Typeface
6+
import android.graphics.fonts.FontFamily
47
import android.util.AttributeSet
8+
import android.util.Log
59
import android.view.LayoutInflater
610
import android.view.View
711
import android.widget.*
812
import androidx.core.content.ContextCompat
913
import androidx.core.content.res.ResourcesCompat
14+
import androidx.core.graphics.drawable.toDrawable
1015
import androidx.core.view.children
1116
import androidx.recyclerview.widget.GridLayoutManager
1217
import androidx.recyclerview.widget.LinearLayoutManager
1318
import androidx.recyclerview.widget.RecyclerView
1419
import com.shahzadafridi.calendarview.Util.dp
20+
import com.shahzadafridi.calendarview.Util.months
21+
import com.shahzadafridi.calendarview.Util.weekDays
22+
import java.lang.Exception
1523
import java.text.SimpleDateFormat
1624
import java.util.*
1725
import kotlin.collections.ArrayList
1826

1927
class CalendarView : LinearLayout, CalenderViewInterface {
2028

29+
val TAG: String = "CalendarView"
30+
2131
// how many days to show, defaults to six weeks, 42 days
2232
private val DAYS_COUNT = 42
2333

2434
//Date format //Default
25-
private var dateFormat: String? = null; private val DATE_FORMAT: String = "yyyy"
35+
private var yearDateFormat: String? = null; private val YEAR_DATE_FORMAT: String = "yyyy"
36+
private var yearTextColor: Int? = null; private val YEAR_TEXT_COLOR: Int = R.color.cblack
37+
private var yearTextSize: Float? = null; private val YEAR_TEXT_SIZE: Float = 16f
38+
private var yearTextFont: Int? = null; private val YEAR_TEXT_FONT: Int = R.font.pfd_cond_regular
39+
40+
//Back Button //Default
41+
private var isBackButtonShow: Boolean? = null; private var IS_BACK_BUTTON_SHOW: Boolean = true
42+
private var backButtonBg: Int? = null; private var BACK_BUTTON_BG: Int = R.drawable.back_icon
2643

2744
//Background //Default
2845
private var cv_bg: Int? = null; private var CALENDER_VIEW_BG: Int = R.drawable.rect_lr_wround_bg
2946

3047
//Month //Default
3148
private var month_font: Int? = null; private var MONTH_FONT: Int = R.font.pfd_cond_regular
32-
private var month_txt_clr: Int? = null; private var MONTH_TEXT_COLOR: Int = R.color.cblack
33-
private var month_txt_size: Int? = null; private var MONTH_TEXT_SIZE: Int = 16
49+
private var month_txt_size: Float? = null; private var MONTH_TEXT_SIZE: Float = 16f
3450
private var month_selected_txt_clr: Int? = null; private var MONTH_SELECTED_TEXT_COLOR: Int = R.color.cblack
3551
private var month_unselected_txt_clr: Int? = null; private var MONTH_UNSELECTED_TEXT_COLOR: Int = R.color.greyed_out
36-
private var month_bg: Int? = null; private var MONTH_BG: Int = R.color.cwhite
52+
private var month_bg: Int? = null; private var MONTH_BG: Int = android.R.color.transparent
3753

3854
//Week //Default
39-
private var week_font: Int? = null; private var WEEK_TEXT_COLOR: Int = R.color.cblack
40-
private var week_txt_clr: Int? = null; private var WEEK_BG_COLOR: Int = R.color.cwhite
41-
private var week_bg_clr: Int? = null; private var WEEK_FONT: Int = R.font.pfd_cond_regular
42-
private var week_txt_size: Int? = null; private var WEEK_TEXT_SIZE: Int = 16
55+
private var week_font: Int? = null; private var WEEK_FONT: Int = R.font.pfd_cond_regular
56+
private var week_txt_clr: Int? = null; private var WEEK_TEXT_COLOR: Int = R.color.cblack
57+
private var week_bg_clr: Int? = null; private var WEEK_BG_COLOR: Int = android.R.color.transparent
58+
private var week_txt_size: Float? = null; private var WEEK_TEXT_SIZE: Float = 16f
4359

4460
//Day //Default
4561
private var day_font: Int? = null; private var DAY_FONT: Int = R.font.pfd_cond_regular
46-
private var day_size: Int? = null; private var DAY_BG: Int = R.color.summer
47-
private var day_bg: Int? = null; private var DAY_SIZE: Int = 14
62+
private var day_size: Float? = null; private var DAY_SIZE: Float = 14f
63+
private var day_bg: Int? = null; private var DAY_BG: Int = android.R.color.transparent
4864
private var day_txt_clr: Int? = null; private var DAY_TEXT_COLOR: Int = R.color.cblack
49-
private var day_txt_size: Int? = null; private var DAY_TEXT_SIZE: Int = 14
65+
private var day_txt_size: Float? = null; private var DAY_TEXT_SIZE: Float = 14f
5066
private var day_selected_txt_clr: Int? = null; private var DAY_SELECTED_TEXT_COLOR: Int = R.color.cwhite
51-
private var day_selected_bg: Int? = null; private var DAY_SELECTED_BG: Int = R.color.cblack
67+
private var day_selected_bg: Int? = null; private var DAY_SELECTED_BG: Int = R.drawable.ic_black_oval
5268

5369
// internal components
5470
private var yearLayout: RelativeLayout? = null
@@ -106,38 +122,39 @@ class CalendarView : LinearLayout, CalenderViewInterface {
106122
buildCalendar()
107123
}
108124

125+
@SuppressLint("ResourceAsColor")
109126
private fun loadDateFormat(attrs: AttributeSet?) {
110127
val calenderViewAttr = context.obtainStyledAttributes(attrs, R.styleable.CalendarView)
111128
try {
112129
//Date Formate
113-
dateFormat = calenderViewAttr.getString(R.styleable.CalendarView_dateFormat)
130+
yearDateFormat = calenderViewAttr.getString(R.styleable.CalendarView_year_date_Formate)
131+
yearTextFont = calenderViewAttr.getResourceId(R.styleable.CalendarView_year_text_font,YEAR_TEXT_FONT)
132+
yearTextColor = calenderViewAttr.getResourceId(R.styleable.CalendarView_year_text_clr,YEAR_TEXT_COLOR)
133+
yearTextSize = calenderViewAttr.getDimension(R.styleable.CalendarView_year_text_size,YEAR_TEXT_SIZE)
134+
//Back
135+
isBackButtonShow = calenderViewAttr.getBoolean(R.styleable.CalendarView_is_back_button_show,IS_BACK_BUTTON_SHOW)
136+
backButtonBg = calenderViewAttr.getResourceId(R.styleable.CalendarView_back_button_bg,BACK_BUTTON_BG)
114137
//Background
115138
cv_bg = calenderViewAttr.getResourceId(R.styleable.CalendarView_cv_bg, CALENDER_VIEW_BG)
116-
/*
117139
//Month
118-
dateFormat = calenderViewAttr.getString(R.styleable.CalendarView_dateFormat)
119-
month_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_font, MONTH_FONT)
120-
month_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_txt_clr, MONTH_TEXT_COLOR)
121-
month_txt_size = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_txt_size, MONTH_TEXT_SIZE)
140+
month_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_font,MONTH_FONT)
141+
month_txt_size = calenderViewAttr.getDimension(R.styleable.CalendarView_month_txt_size, MONTH_TEXT_SIZE)
122142
month_selected_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_selected_txt_clr, MONTH_SELECTED_TEXT_COLOR)
123143
month_unselected_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_unselect_txt_clr, MONTH_UNSELECTED_TEXT_COLOR)
124144
month_bg = calenderViewAttr.getResourceId(R.styleable.CalendarView_month_bg, MONTH_BG)
125145
//Week
126-
week_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_week_font, WEEK_FONT)
146+
week_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_week_font,WEEK_FONT)
127147
week_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_week_txt_clr, WEEK_TEXT_COLOR)
128148
week_bg_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_week_bg_clr, WEEK_BG_COLOR)
129-
week_txt_size = calenderViewAttr.getResourceId(R.styleable.CalendarView_week_txt_size, WEEK_TEXT_SIZE)
149+
week_txt_size = calenderViewAttr.getDimension(R.styleable.CalendarView_week_txt_size, WEEK_TEXT_SIZE)
130150
//Day
131-
day_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_font, DAY_FONT)
132-
day_size = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_size, DAY_SIZE)
151+
day_font = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_font,DAY_FONT)
152+
day_size = calenderViewAttr.getDimension(R.styleable.CalendarView_day_size, DAY_SIZE)
133153
day_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_txt_clr, DAY_TEXT_COLOR)
134-
day_txt_size = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_text_size, DAY_TEXT_SIZE)
154+
day_txt_size = calenderViewAttr.getDimension(R.styleable.CalendarView_day_text_size, DAY_TEXT_SIZE)
135155
day_bg = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_bg, DAY_BG)
136-
day_select_bg = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_select_bg, DAY_SELECTED_BG)
156+
day_selected_bg = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_select_bg, DAY_SELECTED_BG)
137157
day_selected_txt_clr = calenderViewAttr.getResourceId(R.styleable.CalendarView_day_select_txt_clr, DAY_SELECTED_TEXT_COLOR)
138-
139-
*/
140-
141158
} finally {
142159
calenderViewAttr.recycle()
143160
}
@@ -155,6 +172,14 @@ class CalendarView : LinearLayout, CalenderViewInterface {
155172
calendarMonthRv = findViewById(R.id.calendar_month_rv)
156173
calendarMonthRv!!.setHasFixedSize(true)
157174
calendarMonthRv!!.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
175+
withBackButton(isBackButtonShow,backButtonBg)
176+
withYearPanel(yearDateFormat,yearTextColor,yearTextSize,yearTextFont)
177+
withMonthPanel(month_font,month_txt_size,month_selected_txt_clr,month_unselected_txt_clr,month_bg, months)
178+
withWeekPanel(week_font,week_txt_clr,week_txt_size,week_bg_clr, weekDays)
179+
withDayPanel(day_font,day_txt_clr,day_txt_size,day_selected_txt_clr,day_selected_bg,day_bg)
180+
cv_bg?.let {
181+
this.setBackgroundResource(it)
182+
}
158183
}
159184

160185
override fun onMonthClick(view: View?, month: String, position: Int) {
@@ -195,7 +220,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
195220
adapterMonth = MonthAdapter(context,this,monthConfig,Util.months,monthNumber)
196221
calendarMonthRv!!.adapter = adapterMonth
197222

198-
val sdfYear = if (dateFormat != null) SimpleDateFormat(dateFormat) else SimpleDateFormat(DATE_FORMAT)
223+
val sdfYear = if (yearDateFormat != null) SimpleDateFormat(yearDateFormat) else SimpleDateFormat(YEAR_DATE_FORMAT)
199224
yearTv!!.text = sdfYear.format(currentDate.time)
200225

201226
// set header color according to current season
@@ -223,14 +248,15 @@ class CalendarView : LinearLayout, CalenderViewInterface {
223248
return this
224249
}
225250

226-
override fun withBackButton(isShow: Boolean, background: Int?): CalendarView {
251+
override fun withBackButton(isShow: Boolean?, background: Int?): CalendarView {
227252
background?.let {
228253
backIv!!.setImageResource(it)
229254
}
230-
if (isShow){
231-
backIv!!.visibility = View.VISIBLE
232-
}else{
233-
backIv!!.visibility = View.INVISIBLE
255+
isShow?.let {
256+
if (it)
257+
backIv!!.visibility = View.VISIBLE
258+
else
259+
backIv!!.visibility = View.INVISIBLE
234260
}
235261
backIv!!.setOnClickListener { view ->
236262
eventHandler?.onBackClick(view)
@@ -247,18 +273,22 @@ class CalendarView : LinearLayout, CalenderViewInterface {
247273
override fun withYearPanel(
248274
dateFormat: String?,
249275
textColor: Int?,
250-
textSize: Int?,
276+
textSize: Float?,
251277
font: Int?
252278
): CalendarView {
253-
this.dateFormat = dateFormat
279+
this.yearDateFormat = dateFormat
254280
textSize?.let {
255-
yearTv!!.textSize = it.toFloat()
281+
yearTv!!.textSize = it
256282
}
257283
textColor?.let { clr ->
258284
yearTv!!.setTextColor(ContextCompat.getColor(context, clr))
259285
}
260286
font?.let {
261-
yearTv!!.typeface = ResourcesCompat.getFont(context, it)
287+
try {
288+
yearTv!!.typeface = ResourcesCompat.getFont(context, it)
289+
}catch (e: Exception){
290+
Log.e(TAG, "$font not found!")
291+
}
262292
}
263293
return this
264294
}
@@ -275,28 +305,23 @@ class CalendarView : LinearLayout, CalenderViewInterface {
275305

276306
override fun withMonthPanel(
277307
font: Int?,
278-
textSize: Int?,
308+
textSize: Float?,
279309
selectedTextColor: Int?,
280310
unSelectedTextColor: Int?,
281311
background: Int?,
282312
months: ArrayList<String>?
283313
): CalendarView {
284-
if (months != null) {
285-
Util.months = months
286-
}
287314
month_font = font
288315
month_txt_size = textSize
289316
month_selected_txt_clr = selectedTextColor
290317
month_unselected_txt_clr = unSelectedTextColor
291318
month_bg = background
319+
if (months != null) Util.months = months
292320
monthConfig.mBg = month_bg
293321
monthConfig.mSelectedClr = month_selected_txt_clr
294322
monthConfig.mUnSelectedClr = month_unselected_txt_clr
295323
monthConfig.mFont = month_font
296324
monthConfig.mTxtSize = month_txt_size
297-
month_font?.let {
298-
yearTv!!.typeface = ResourcesCompat.getFont(context, it)
299-
}
300325
month_bg?.let {
301326
calendarMonthRv!!.background = ContextCompat.getDrawable(context, it)
302327
}
@@ -313,7 +338,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
313338
return this
314339
}
315340

316-
override fun withWeekPanel(font: Int?, textColor: Int?, textSize: Int?, background: Int?,weekDays: ArrayList<String>?): CalendarView {
341+
override fun withWeekPanel(font: Int?, textColor: Int?, textSize: Float?, background: Int?,weekDays: ArrayList<String>?): CalendarView {
317342
if (weekDays != null) {
318343
Util.weekDays = weekDays
319344
}
@@ -323,16 +348,17 @@ class CalendarView : LinearLayout, CalenderViewInterface {
323348
week_bg_clr = background
324349

325350
var i = 0
326-
327351
weekLayout!!.children.iterator().forEach {
328352
week_font?.let { font ->
329-
(it as TextView).typeface = ResourcesCompat.getFont(context, font)
353+
try {
354+
(it as TextView).typeface = ResourcesCompat.getFont(context, font)
355+
}catch (e: Exception){ Log.e(TAG, "$font not found!") }
330356
}
331357
week_txt_clr?.let { clr ->
332358
(it as TextView).setTextColor(ContextCompat.getColor(context, clr))
333359
}
334360
week_txt_size?.let { size ->
335-
(it as TextView).setTextSize(size.toFloat())
361+
(it as TextView).setTextSize(size)
336362
}
337363
(it as TextView).text = Util.weekDays[i]
338364
i++
@@ -358,7 +384,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
358384
override fun withDayPanel(
359385
font: Int?,
360386
textColor: Int?,
361-
textSize: Int?,
387+
textSize: Float?,
362388
selectedTextColor: Int?,
363389
selectedBackground: Int?,
364390
background: Int?

0 commit comments

Comments
 (0)