Skip to content

Commit 67a8772

Browse files
committed
show half hour marks on the left-hand column
1 parent ed450d8 commit 67a8772

File tree

7 files changed

+73
-16
lines changed

7 files changed

+73
-16
lines changed

library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
public interface DateTimeInterpreter {
99
String interpretDate(Calendar date);
10-
String interpretTime(int hour);
10+
String interpretTime(int hour, int minutes);
1111
}

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private enum Direction {
142142
private boolean mShowDistinctPastFutureColor = false;
143143
private boolean mHorizontalFlingEnabled = true;
144144
private boolean mVerticalFlingEnabled = true;
145+
private boolean showHalfHours = false;
145146
private int mAllDayEventHeight = 100;
146147
private int mScrollDuration = 250;
147148

@@ -351,6 +352,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
351352
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);
352353
mHorizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, mHorizontalFlingEnabled);
353354
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
355+
showHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, showHalfHours);
354356
mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight);
355357
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
356358
} finally {
@@ -374,7 +376,9 @@ private void init() {
374376
mTimeTextPaint.setTextSize(mTextSize);
375377
mTimeTextPaint.setColor(mHeaderColumnTextColor);
376378
Rect rect = new Rect();
377-
mTimeTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
379+
final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM";
380+
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
381+
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
378382
mTimeTextHeight = rect.height();
379383
mHeaderMarginBottom = mTimeTextHeight / 2;
380384
initTextTimeWidth();
@@ -384,7 +388,7 @@ private void init() {
384388
mHeaderTextPaint.setColor(mHeaderColumnTextColor);
385389
mHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
386390
mHeaderTextPaint.setTextSize(mTextSize);
387-
mHeaderTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
391+
mHeaderTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
388392
mHeaderTextHeight = rect.height();
389393
mHeaderTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
390394

@@ -479,7 +483,7 @@ private void initTextTimeWidth() {
479483
mTimeTextWidth = 0;
480484
for (int i = 0; i < 24; i++) {
481485
// Measure time string and get max width.
482-
String time = getDateTimeInterpreter().interpretTime(i);
486+
String time = getDateTimeInterpreter().interpretTime(i, 0);
483487
if (time == null)
484488
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
485489
mTimeTextWidth = Math.max(mTimeTextWidth, mTimeTextPaint.measureText(time));
@@ -533,11 +537,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
533537
// Clip to paint in left column only.
534538
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE);
535539

536-
for (int i = 0; i < 24; i++) {
537-
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
540+
int numPeriodsInDay = showHalfHours ? 48 : 24;
541+
for (int i = 0; i < numPeriodsInDay; i++) {
542+
// If we are showing half hours (eg. 5:30am), space the times out by half the hour height
543+
// and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
544+
int timeSpacing;
545+
int minutes;
546+
int hour;
547+
if (showHalfHours) {
548+
timeSpacing = mHourHeight / 2;
549+
hour = i / 2;
550+
if (i % 2 == 0) {
551+
minutes = 0;
552+
} else {
553+
minutes = 30;
554+
}
555+
} else {
556+
timeSpacing = mHourHeight;
557+
hour = i;
558+
minutes = 0;
559+
}
560+
561+
// Calculate the top of the rectangle where the time text will go
562+
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
563+
564+
// Get the time to be displayed, as a String.
565+
String time = getDateTimeInterpreter().interpretTime(hour, minutes);
538566

539567
// Draw the text if its y position is not outside of the visible area. The pivot point of the text is the point at the bottom-right corner.
540-
String time = getDateTimeInterpreter().interpretTime(i);
541568
if (time == null)
542569
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
543570
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
@@ -1311,13 +1338,22 @@ public String interpretDate(Calendar date) {
13111338
}
13121339

13131340
@Override
1314-
public String interpretTime(int hour) {
1341+
public String interpretTime(int hour, int minutes) {
13151342
Calendar calendar = Calendar.getInstance();
13161343
calendar.set(Calendar.HOUR_OF_DAY, hour);
1317-
calendar.set(Calendar.MINUTE, 0);
1344+
calendar.set(Calendar.MINUTE, minutes);
13181345

13191346
try {
1320-
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
1347+
SimpleDateFormat sdf;
1348+
if (DateFormat.is24HourFormat(getContext())) {
1349+
sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
1350+
} else {
1351+
if (showHalfHours) {
1352+
sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
1353+
} else {
1354+
sdf = new SimpleDateFormat("hh a", Locale.getDefault());
1355+
}
1356+
}
13211357
return sdf.format(calendar.getTime());
13221358
} catch (Exception e) {
13231359
e.printStackTrace();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<attr name="columnGap" format="dimension"/>
2121
<attr name="headerColumnTextColor" format="color"/>
2222
<attr name="noOfVisibleDays" format="integer"/>
23-
<attr name="showFirstDayOfWeekFirst" format="boolean"/>
2423
<attr name="headerRowBackgroundColor" format="color"/>
2524
<attr name="dayBackgroundColor" format="color"/>
2625
<attr name="hourSeparatorColor" format="color"/>
@@ -49,6 +48,7 @@
4948
<attr name="nowLineThickness" format="dimension"/>
5049
<attr name="horizontalFlingEnabled" format="boolean"/>
5150
<attr name="verticalFlingEnabled" format="boolean"/>
51+
<attr name="showHalfHours" format="boolean" />
5252
<attr name="allDayEventHeight" format="dimension"/>
5353
<attr name="scrollDuration" format="integer"/>
5454
</declare-styleable>

sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,17 @@ public String interpretDate(Calendar date) {
135135
}
136136

137137
@Override
138-
public String interpretTime(int hour) {
139-
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
138+
public String interpretTime(int hour, int minutes) {
139+
String strMinutes = String.format("%02d", minutes);
140+
if (hour > 11) {
141+
return (hour - 12) + ":" + strMinutes + " PM";
142+
} else {
143+
if (hour == 0) {
144+
return "12:" + strMinutes + " AM";
145+
} else {
146+
return hour + ":" + strMinutes + " AM";
147+
}
148+
}
140149
}
141150
});
142151
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
157157
event.setColor(getResources().getColor(R.color.event_color_01));
158158
events.add(event);
159159

160+
startTime = Calendar.getInstance();
161+
startTime.set(Calendar.HOUR_OF_DAY, 18);
162+
startTime.set(Calendar.MINUTE, 30);
163+
startTime.set(Calendar.MONTH, newMonth-1);
164+
startTime.set(Calendar.YEAR, newYear);
165+
endTime = (Calendar) startTime.clone();
166+
endTime.set(Calendar.HOUR_OF_DAY, 19);
167+
endTime.set(Calendar.MINUTE, 30);
168+
endTime.set(Calendar.MONTH, newMonth-1);
169+
event = new WeekViewEvent(22, getEventTitle(startTime), startTime, endTime);
170+
event.setColor(getResources().getColor(R.color.event_color_02));
171+
events.add(event);
172+
160173
return events;
161174
}
162175

sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.view.View;
77

8-
98
/**
109
* The launcher activity of the sample app. It contains the links to visit all the example screens.
1110
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
@@ -34,5 +33,4 @@ public void onClick(View v) {
3433
}
3534
});
3635
}
37-
3836
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
app:dayBackgroundColor="#05000000"
2323
app:todayBackgroundColor="#1848adff"
2424
app:headerColumnBackground="#ffffffff"
25-
app:todayHeaderTextColor="@color/accent" />
25+
app:todayHeaderTextColor="@color/accent"
26+
app:showHalfHours="false" />
2627

2728
</RelativeLayout>

0 commit comments

Comments
 (0)