Skip to content

Commit 610bd21

Browse files
added feature to set a default title for events that don't have a title, using untitledEventText
1 parent eacd593 commit 610bd21

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
252252
field = value
253253
invalidate()
254254
}
255+
var untitledEventText: String? = null
256+
set(value) {
257+
if (field == value)
258+
return
259+
field = value
260+
invalidate()
261+
}
255262

256263
private val mGestureListener = object : GestureDetector.SimpleOnGestureListener() {
257264
override fun onDown(e: MotionEvent): Boolean {
@@ -1061,6 +1068,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10611068
headerWeekDayTitleTextSize = a.getDimension(R.styleable.WeekView_headerWeekDayTitleTextSize, headerWeekDayTitleTextSize)
10621069
headerWeekDaySubtitleTextSize = a.getDimension(R.styleable.WeekView_headerWeekDaySubtitleTextSize, headerWeekDaySubtitleTextSize)
10631070
spaceBetweenHeaderWeekDayTitleAndSubtitle = a.getDimensionPixelSize(R.styleable.WeekView_spaceBetweenHeaderWeekDayTitleAndSubtitle, spaceBetweenHeaderWeekDayTitleAndSubtitle)
1071+
untitledEventText = a.getString(R.styleable.WeekView_untitledEventText) ?: untitledEventText
10641072
} finally {
10651073
a.recycle()
10661074
}
@@ -1745,8 +1753,10 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
17451753

17461754
// Prepare the name of the event.
17471755
val bob = SpannableStringBuilder()
1748-
if (!TextUtils.isEmpty(event.name)) {
1749-
bob.append(event.name)
1756+
if (!TextUtils.isEmpty(event.name) || !TextUtils.isEmpty(untitledEventText)) {
1757+
if (!TextUtils.isEmpty(event.name))
1758+
bob.append(event.name)
1759+
else bob.append(untitledEventText)
17501760
bob.setSpan(StyleSpan(android.graphics.Typeface.BOLD), 0, bob.length, 0)
17511761
}
17521762
// Prepare the location of the event.

library/src/main/java/com/alamkanak/weekview/WeekViewEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ open class WeekViewEvent {
5959
* @param startTime The time when the event starts.
6060
* @param endTime The time when the event ends.
6161
*/
62-
constructor(id: String?, name: String, startTime: Calendar, endTime: Calendar) : this(id, name, null, startTime, endTime)
62+
constructor(id: String?, name: String?, startTime: Calendar, endTime: Calendar) : this(id, name, null, startTime, endTime)
6363

6464
override fun equals(other: Any?): Boolean {
6565
if (this === other) return true

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@
6868
<attr name="headerWeekDayTitleTextSize" format="dimension"/>
6969
<attr name="headerWeekDaySubtitleTextSize" format="dimension"/>
7070
<attr name="spaceBetweenHeaderWeekDayTitleAndSubtitle" format="dimension"/>
71-
71+
<attr name="untitledEventText" format="string"/>
7272
</declare-styleable>
7373
</resources>

sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ open class BasicActivity : BaseActivity() {
3333
endTime.add(Calendar.HOUR, 1)
3434
endTime.set(Calendar.MONTH, newMonth - 1)
3535
var event = WeekViewEvent("First", getEventTitle(startTime, endTime), startTime, endTime)
36-
event.color = resources.getColor(R.color.event_color_01)
36+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_01, null)
3737
events.add(event)
3838

3939
startTime = Calendar.getInstance()
@@ -58,7 +58,7 @@ open class BasicActivity : BaseActivity() {
5858
endTime.set(Calendar.MINUTE, 30)
5959
endTime.set(Calendar.MONTH, newMonth - 1)
6060
event = WeekViewEvent("Second", getEventTitle(startTime, endTime), startTime, endTime)
61-
event.color = resources.getColor(R.color.event_color_05)
61+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_05, null)
6262
events.add(event)
6363

6464
startTime = Calendar.getInstance()
@@ -70,7 +70,7 @@ open class BasicActivity : BaseActivity() {
7070
endTime.set(Calendar.HOUR_OF_DAY, 5)
7171
endTime.set(Calendar.MINUTE, 0)
7272
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
73-
event.color = resources.getColor(R.color.event_color_03)
73+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_03, null)
7474
events.add(event)
7575

7676
startTime = Calendar.getInstance()
@@ -82,7 +82,7 @@ open class BasicActivity : BaseActivity() {
8282
endTime.add(Calendar.HOUR_OF_DAY, 2)
8383
endTime.set(Calendar.MONTH, newMonth - 1)
8484
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
85-
event.color = resources.getColor(R.color.event_color_02)
85+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_02, null)
8686
events.add(event)
8787

8888
startTime = Calendar.getInstance()
@@ -95,7 +95,7 @@ open class BasicActivity : BaseActivity() {
9595
endTime.add(Calendar.HOUR_OF_DAY, 3)
9696
endTime.set(Calendar.MONTH, newMonth - 1)
9797
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
98-
event.color = resources.getColor(R.color.event_color_03)
98+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_03, null)
9999
events.add(event)
100100

101101
startTime = Calendar.getInstance()
@@ -107,7 +107,7 @@ open class BasicActivity : BaseActivity() {
107107
endTime = startTime.clone() as Calendar
108108
endTime.add(Calendar.HOUR_OF_DAY, 3)
109109
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
110-
event.color = resources.getColor(R.color.event_color_04)
110+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_04, null)
111111
events.add(event)
112112

113113
startTime = Calendar.getInstance()
@@ -119,7 +119,7 @@ open class BasicActivity : BaseActivity() {
119119
endTime = startTime.clone() as Calendar
120120
endTime.add(Calendar.HOUR_OF_DAY, 3)
121121
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
122-
event.color = resources.getColor(R.color.event_color_01)
122+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_01, null)
123123
events.add(event)
124124

125125
startTime = Calendar.getInstance()
@@ -131,7 +131,7 @@ open class BasicActivity : BaseActivity() {
131131
endTime = startTime.clone() as Calendar
132132
endTime.add(Calendar.HOUR_OF_DAY, 3)
133133
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime), startTime, endTime)
134-
event.color = resources.getColor(R.color.event_color_02)
134+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_02, null)
135135
events.add(event)
136136

137137
//AllDay event
@@ -142,7 +142,7 @@ open class BasicActivity : BaseActivity() {
142142
startTime.set(Calendar.MONTH, newMonth - 1)
143143
startTime.set(Calendar.YEAR, newYear)
144144
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, allDay = true), null, startTime)
145-
event.color = resources.getColor(R.color.event_color_04)
145+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_04, null)
146146
events.add(event)
147147

148148
startTime = Calendar.getInstance()
@@ -155,7 +155,7 @@ open class BasicActivity : BaseActivity() {
155155
endTime.set(Calendar.DAY_OF_MONTH, 10)
156156
endTime.set(Calendar.HOUR_OF_DAY, 23)
157157
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime, true), null, startTime, endTime, true)
158-
event.color = resources.getColor(R.color.event_color_03)
158+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_03, null)
159159
events.add(event)
160160

161161
// All day event until 00:00 next day
@@ -170,7 +170,7 @@ open class BasicActivity : BaseActivity() {
170170
endTime = startTime.clone() as Calendar
171171
endTime.set(Calendar.DAY_OF_MONTH, 11)
172172
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime, true), null, startTime, endTime, true)
173-
event.color = resources.getColor(R.color.event_color_01)
173+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_01, null)
174174

175175
startTime = Calendar.getInstance()
176176
startTime.set(Calendar.HOUR_OF_DAY, 18)
@@ -182,7 +182,7 @@ open class BasicActivity : BaseActivity() {
182182
endTime.set(Calendar.MINUTE, 30)
183183
endTime.set(Calendar.MONTH, newMonth - 1)
184184
event = WeekViewEvent(getUniqueId(), getEventTitle(startTime, endTime, true), null, startTime, endTime, true)
185-
event.color = resources.getColor(R.color.event_color_02)
185+
event.color = ResourcesCompat.getColor(resources, R.color.event_color_02, null)
186186
events.add(event)
187187

188188
return events

sample/src/main/res/layout/activity_base.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
<com.alamkanak.weekview.WeekView
77
android:id="@+id/weekView" android:layout_width="match_parent" android:layout_height="match_parent"
8-
app:autoLimitTime="false" app:dayBackgroundColor="#05000000" app:headerColumnTextColor="@color/toolbar_text" app:headerRowBackgroundColor="@color/toolbar"
9-
app:maxTime="16" app:minOverlappingMinutes="5" app:minTime="4"
10-
app:newEventTimeResolutionInMinutes="15" app:noOfVisibleDays="3"
11-
app:timeColumnResolution="60" app:todayColumnBackgroundColor="#1848adff" app:todayHeaderTextColor="@color/accent"/>
8+
app:autoLimitTime="false" app:dayBackgroundColor="#05000000" app:headerColumnTextColor="@color/toolbar_text"
9+
app:headerRowBackgroundColor="@color/toolbar" app:maxTime="16" app:minOverlappingMinutes="5" app:minTime="4"
10+
app:newEventTimeResolutionInMinutes="15" app:noOfVisibleDays="3" app:timeColumnResolution="60"
11+
app:todayColumnBackgroundColor="#1848adff" app:todayHeaderTextColor="@color/accent"
12+
app:untitledEventText="@string/untitled_event"/>
1213

1314
<TextView
1415
android:id="@+id/draggable_view" android:layout_width="wrap_content" android:layout_height="wrap_content"

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<string name="drag_me">Drag Me</string>
1212
<string name="title_activity_whole_view_snap">Whole view snap</string>
1313
<string name="all_day">All day</string>
14+
<string name="untitled_event">Untitled event</string>
1415
</resources>

0 commit comments

Comments
 (0)