Skip to content

Commit b43621b

Browse files
committed
Selected date background change.
1 parent 3ab3caf commit b43621b

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.shahzadafridi.calendarview
22

33
import android.content.Context
4-
import android.graphics.Color
5-
import android.graphics.Typeface
64
import android.graphics.drawable.ShapeDrawable
75
import android.graphics.drawable.shapes.RoundRectShape
86
import android.util.Log
@@ -17,14 +15,14 @@ import androidx.recyclerview.widget.RecyclerView
1715
import java.lang.Exception
1816
import java.util.*
1917

20-
2118
class CalendarAdapter(
2219
context: Context,
2320
private val days: ArrayList<Calendar>, // days with events
2421
private val eventDays: HashSet<Calendar>?,
2522
private var eventsHandler: CalenderViewInterface.EventHandler?,
2623
private var dayConfig: DayConfiguration?,
27-
monthNumber: Int
24+
monthNumber: Int,
25+
private var selectedDate: Calendar?
2826
) : RecyclerView.Adapter<CalendarAdapter.MyViewHolder>() {
2927

3028
val TAG: String = "CalendarAdapter"
@@ -113,19 +111,16 @@ class CalendarAdapter(
113111
val month = date.get(Calendar.MONTH)
114112
val year = date.get(Calendar.YEAR)
115113

116-
// today
117-
val today = Calendar.getInstance()
118-
119114
// set text
120115
holder.textView.text = date.get(Calendar.DATE).toString()
121116

122117

123-
if (month != mMonthNumber || year != today.get(Calendar.YEAR)) {
118+
if (month != mMonthNumber || year != selectedDate!!.get(Calendar.YEAR)) {
124119
// if this day is outside current month, grey it out
125120
holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.greyed_out))
126121
holder.rowLayout.isEnabled = false
127-
} else if (today.get(Calendar.DATE) == day && today.get(Calendar.MONTH) == month && today.get(Calendar.YEAR) == year) {
128-
// if it is today, set it to blue/bold
122+
} else if (selectedDate!!.get(Calendar.DATE) == day && selectedDate!!.get(Calendar.MONTH) == month && selectedDate!!.get(Calendar.YEAR) == year) {
123+
// if it is today, set it to color/bold
129124
day_selected_txt_clr?.let {
130125
holder.textView.setTextColor(ContextCompat.getColor(mContext, it))
131126
} ?: run{
@@ -153,7 +148,9 @@ class CalendarAdapter(
153148
}
154149

155150
override fun onClick(v: View?) {
151+
selectedDate = days[adapterPosition]
156152
eventsHandler?.onDayClick(view = v, date = days[adapterPosition].time, adapterPosition)
153+
notifyDataSetChanged()
157154
}
158155

159156
override fun onLongClick(v: View?): Boolean {
@@ -166,7 +163,9 @@ class CalendarAdapter(
166163
}
167164
}
168165

169-
fun setEventHandler(mEventsHandler: CalenderViewInterface.EventHandler) {
166+
fun setEventHandler(
167+
mEventsHandler: CalenderViewInterface.EventHandler
168+
) {
170169
this.eventsHandler = mEventsHandler
171170
}
172171

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package com.shahzadafridi.calendarview
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5-
import android.graphics.Typeface
6-
import android.graphics.fonts.FontFamily
75
import android.util.AttributeSet
86
import android.util.Log
97
import android.view.LayoutInflater
108
import android.view.View
119
import android.widget.*
1210
import androidx.core.content.ContextCompat
1311
import androidx.core.content.res.ResourcesCompat
14-
import androidx.core.graphics.drawable.toDrawable
1512
import androidx.core.view.children
1613
import androidx.recyclerview.widget.GridLayoutManager
1714
import androidx.recyclerview.widget.LinearLayoutManager
@@ -66,6 +63,9 @@ class CalendarView : LinearLayout, CalenderViewInterface {
6663
private var day_selected_txt_clr: Int? = null; private var DAY_SELECTED_TEXT_COLOR: Int = R.color.cwhite
6764
private var day_selected_bg: Int? = null; private var DAY_SELECTED_BG: Int = R.drawable.ic_black_oval
6865

66+
//Selected Date default Today
67+
private var selectedDate: Calendar = Calendar.getInstance()
68+
6969
// internal components
7070
private var yearLayout: RelativeLayout? = null
7171
private var backIv: ImageView? = null
@@ -213,7 +213,7 @@ class CalendarView : LinearLayout, CalenderViewInterface {
213213
}
214214

215215
// update grid
216-
adapter = CalendarAdapter(context, cells, events, eventHandler, dayConfig,monthNumber)
216+
adapter = CalendarAdapter(context, cells, events, eventHandler, dayConfig,monthNumber,selectedDate)
217217
calendarDayRv!!.adapter = adapter
218218

219219
//update months
@@ -270,6 +270,11 @@ class CalendarView : LinearLayout, CalenderViewInterface {
270270
return this
271271
}
272272

273+
override fun withUpdateSelectDate(calendar: Calendar): CalendarView {
274+
this.selectedDate = calendar
275+
return this
276+
}
277+
273278
override fun withYearPanel(
274279
dateFormat: String?,
275280
textColor: Int?,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface CalenderViewInterface {
1616
fun withDayPanel(font: Int? = null, textColor: Int? = null, textSize: Float? = null, selectedTextColor: Int? = null, selectedBackground:Int? = null, background: Int? = null): CalendarView
1717
fun withDayPanelMargin(top: Int = 0,bottom: Int = 0,left: Int = 0,right: Int = 0): CalendarView
1818
fun withCalenderViewBg(background: Int? = null): CalendarView
19+
fun withUpdateSelectDate(selectedDate: Calendar): CalendarView
1920
fun buildCalendar(): CalendarView
2021
fun onMonthClick(view: View?, month: String, position: Int)
2122
interface EventHandler{

app/src/main/java/com/shahzadafridi/sample/MainActivity.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ class MainActivity : AppCompatActivity() {
5050
add("SAT")
5151
}
5252

53+
var selectedDate: Calendar? = null
54+
5355
override fun onCreate(savedInstanceState: Bundle?) {
5456
super.onCreate(savedInstanceState)
5557
binding = ActivityMainBinding.inflate(layoutInflater)
5658
setContentView(binding.root)
5759
//@isXMLConfiguration true because values setup in XML
60+
selectedDate = Calendar.getInstance()
5861
setUpCalendarView(binding.calendarView,true)
5962
}
6063

@@ -127,6 +130,7 @@ class MainActivity : AppCompatActivity() {
127130
.withCalenderViewBg(
128131
background = R.drawable.rect_lr_wround_bg
129132
)
133+
.withUpdateSelectDate(selectedDate!!)
130134
.withEvents(
131135
events = events,
132136
eventDotColor = R.color.green
@@ -139,6 +143,9 @@ class MainActivity : AppCompatActivity() {
139143

140144
override fun onDayClick(view: View?, date: Date, position: Int) {
141145
val df = SimpleDateFormat.getDateInstance()
146+
selectedDate = Calendar.getInstance().apply {
147+
time = date
148+
}
142149
Toast.makeText(this@MainActivity, df.format(date), Toast.LENGTH_SHORT).show()
143150
Log.e("TEST", "onDayClick")
144151
}

app/src/main/res/layout/activity_main.xml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
android:id="@+id/calendar_view"
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
14-
app:back_button_bg="@drawable/ic_up_round"
15-
app:cv_bg="@drawable/rect_lr_wround_bg"
16-
app:day_bg="@color/white"
17-
app:day_font="@font/titillium_web_semibold"
18-
app:day_select_bg="@drawable/ic_green_oval"
19-
app:day_select_txt_clr="@color/white"
20-
app:day_text_size="5dp"
21-
app:day_txt_clr="@color/cblack"
22-
app:is_back_button_show="true"
2314
app:layout_constraintEnd_toEndOf="parent"
2415
app:layout_constraintStart_toStartOf="parent"
2516
app:layout_constraintTop_toTopOf="parent"
26-
app:month_bg="@color/black"
27-
app:month_font="@font/titillium_web_semibold"
28-
app:month_selected_txt_clr="@color/green"
17+
app:is_back_button_show="true"
18+
app:back_button_bg="@drawable/ic_up_round"
19+
app:year_date_Formate="MM YYYY"
20+
app:year_text_font="@font/titillium_web_semibold"
21+
app:year_text_clr="@color/cblack"
22+
app:year_text_size="12dp"
2923
app:month_txt_size="8dp"
24+
app:month_font="@font/titillium_web_semibold"
3025
app:month_unselect_txt_clr="@color/white"
31-
app:week_bg_clr="@color/black"
26+
app:month_selected_txt_clr="@color/green"
27+
app:month_bg="@color/black"
3228
app:week_font="@font/titillium_web_semibold"
29+
app:week_bg_clr="@color/black"
3330
app:week_txt_clr="@color/white"
3431
app:week_txt_size="6dp"
35-
app:year_date_Formate="MM YYYY"
36-
app:year_text_clr="@color/cblack"
37-
app:year_text_font="@font/titillium_web_semibold"
38-
app:year_text_size="12dp" />
32+
app:day_bg="@color/white"
33+
app:day_select_bg="@drawable/ic_green_oval"
34+
app:day_text_size="5dp"
35+
app:day_select_txt_clr="@color/white"
36+
app:day_txt_clr="@color/cblack"
37+
app:day_font="@font/titillium_web_semibold"
38+
app:cv_bg="@drawable/rect_lr_wround_bg"/>
3939

4040
<Button
4141
android:id="@+id/showDialogCalendar"

0 commit comments

Comments
 (0)