Skip to content

Commit 528565c

Browse files
committed
show half hour marks on the left-hand column
1 parent 2f1e6e3 commit 528565c

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public class WeekView extends View {
127127
private boolean mShowDistinctWeekendColor = false;
128128
private boolean mShowNowLine = false;
129129
private boolean mShowDistinctPastFutureColor = false;
130+
private boolean showHalfHours = false;
130131

131132
// Listeners.
132133
private EventClickListener mEventClickListener;
@@ -303,7 +304,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
303304
mShowDistinctPastFutureColor = a.getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, mShowDistinctPastFutureColor);
304305
mShowDistinctWeekendColor = a.getBoolean(R.styleable.WeekView_showDistinctWeekendColor, mShowDistinctWeekendColor);
305306
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);
306-
307+
showHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, showHalfHours);
307308
} finally {
308309
a.recycle();
309310
}
@@ -322,7 +323,9 @@ private void init() {
322323
mTimeTextPaint.setTextSize(mTextSize);
323324
mTimeTextPaint.setColor(mHeaderColumnTextColor);
324325
Rect rect = new Rect();
325-
mTimeTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
326+
final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM";
327+
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
328+
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
326329
mTimeTextHeight = rect.height();
327330
mHeaderMarginBottom = mTimeTextHeight / 2;
328331
initTextTimeWidth();
@@ -332,7 +335,7 @@ private void init() {
332335
mHeaderTextPaint.setColor(mHeaderColumnTextColor);
333336
mHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
334337
mHeaderTextPaint.setTextSize(mTextSize);
335-
mHeaderTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
338+
mHeaderTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
336339
mHeaderTextHeight = rect.height();
337340
mHeaderTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
338341

@@ -427,7 +430,7 @@ private void initTextTimeWidth() {
427430
mTimeTextWidth = 0;
428431
for (int i = 0; i < 24; i++) {
429432
// Measure time string and get max width.
430-
String time = getDateTimeInterpreter().interpretTime(i);
433+
String time = getDateTimeInterpreter().interpretTime(i, 0);
431434
if (time == null)
432435
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
433436
mTimeTextWidth = Math.max(mTimeTextWidth, mTimeTextPaint.measureText(time));
@@ -455,11 +458,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
455458
// Draw the background color for the header column.
456459
canvas.drawRect(0, mHeaderTextHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), mHeaderColumnBackgroundPaint);
457460

458-
for (int i = 0; i < 24; i++) {
459-
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
461+
int numPeriodsInDay = showHalfHours ? 48 : 24;
462+
for (int i = 0; i < numPeriodsInDay; i++) {
463+
// If we are showing half hours (eg. 5:30am), space the times out by half the hour height
464+
// and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
465+
int timeSpacing;
466+
int minutes;
467+
int hour;
468+
if (showHalfHours) {
469+
timeSpacing = mHourHeight / 2;
470+
hour = i / 2;
471+
if (i % 2 == 0) {
472+
minutes = 0;
473+
} else {
474+
minutes = 30;
475+
}
476+
} else {
477+
timeSpacing = mHourHeight;
478+
hour = i;
479+
minutes = 0;
480+
}
481+
482+
// Calculate the top of the rectangle where the time text will go
483+
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + timeSpacing * i + mHeaderMarginBottom;
484+
485+
// Get the time to be displayed, as a String.
486+
String time = getDateTimeInterpreter().interpretTime(hour, minutes);
460487

461488
// 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.
462-
String time = getDateTimeInterpreter().interpretTime(i);
463489
if (time == null)
464490
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
465491
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
@@ -1201,13 +1227,22 @@ public String interpretDate(Calendar date) {
12011227
}
12021228

12031229
@Override
1204-
public String interpretTime(int hour) {
1230+
public String interpretTime(int hour, int minutes) {
12051231
Calendar calendar = Calendar.getInstance();
12061232
calendar.set(Calendar.HOUR_OF_DAY, hour);
1207-
calendar.set(Calendar.MINUTE, 0);
1233+
calendar.set(Calendar.MINUTE, minutes);
12081234

12091235
try {
1210-
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
1236+
SimpleDateFormat sdf;
1237+
if (DateFormat.is24HourFormat(getContext())) {
1238+
sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
1239+
} else {
1240+
if (showHalfHours) {
1241+
sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
1242+
} else {
1243+
sdf = new SimpleDateFormat("hh a", Locale.getDefault());
1244+
}
1245+
}
12111246
return sdf.format(calendar.getTime());
12121247
} catch (Exception e) {
12131248
e.printStackTrace();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
<attr name="showNowLine" format="boolean"/>
4747
<attr name="nowLineColor" format="color"/>
4848
<attr name="nowLineThickness" format="dimension"/>
49+
<attr name="showHalfHours" format="boolean" />
4950
</declare-styleable>
50-
</resources>
51+
</resources>

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.RectF;
44
import android.os.Bundle;
55
import android.support.v7.app.ActionBarActivity;
6+
import android.util.Log;
67
import android.util.TypedValue;
78
import android.view.Menu;
89
import android.view.MenuItem;
@@ -13,6 +14,7 @@
1314
import com.alamkanak.weekview.WeekView;
1415
import com.alamkanak.weekview.WeekViewEvent;
1516

17+
import java.text.Format;
1618
import java.text.SimpleDateFormat;
1719
import java.util.ArrayList;
1820
import java.util.Calendar;
@@ -134,8 +136,18 @@ public String interpretDate(Calendar date) {
134136
}
135137

136138
@Override
137-
public String interpretTime(int hour) {
138-
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
139+
public String interpretTime(int hour, int minutes) {
140+
String strMinutes = String.format("%02d", minutes);
141+
if (hour > 11) {
142+
return (hour - 12) + ":" + strMinutes + " PM";
143+
}
144+
else {
145+
if (hour == 0) {
146+
return "12:" + strMinutes + " AM";
147+
} else {
148+
return hour + ":" + strMinutes + " AM";
149+
}
150+
}
139151
}
140152
});
141153
}
@@ -244,6 +256,19 @@ public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
244256
event.setColor(getResources().getColor(R.color.event_color_02));
245257
events.add(event);
246258

259+
startTime = Calendar.getInstance();
260+
startTime.set(Calendar.HOUR_OF_DAY, 18);
261+
startTime.set(Calendar.MINUTE, 30);
262+
startTime.set(Calendar.MONTH, newMonth-1);
263+
startTime.set(Calendar.YEAR, newYear);
264+
endTime = (Calendar) startTime.clone();
265+
endTime.set(Calendar.HOUR_OF_DAY, 19);
266+
endTime.set(Calendar.MINUTE, 30);
267+
endTime.set(Calendar.MONTH, newMonth-1);
268+
event = new WeekViewEvent(22, getEventTitle(startTime), startTime, endTime);
269+
event.setColor(getResources().getColor(R.color.event_color_02));
270+
events.add(event);
271+
247272
return events;
248273
}
249274

sample/src/main/res/layout/activity_main.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)