@@ -128,6 +128,7 @@ public class WeekView extends View {
128
128
private ScaleGestureDetector mScaleDetector ;
129
129
private boolean mIsZooming ;
130
130
private int mEventCornerRadius = 0 ;
131
+ private boolean showHalfHours = false ;
131
132
132
133
// Listeners.
133
134
private EventClickListener mEventClickListener ;
@@ -291,6 +292,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
291
292
mEventMarginVertical = a .getDimensionPixelSize (R .styleable .WeekView_eventMarginVertical , mEventMarginVertical );
292
293
mXScrollingSpeed = a .getFloat (R .styleable .WeekView_xScrollingSpeed , mXScrollingSpeed );
293
294
mEventCornerRadius = a .getDimensionPixelSize (R .styleable .WeekView_eventCornerRadius , mEventCornerRadius );
295
+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
294
296
} finally {
295
297
a .recycle ();
296
298
}
@@ -310,7 +312,9 @@ private void init() {
310
312
mTimeTextPaint .setTextSize (mTextSize );
311
313
mTimeTextPaint .setColor (mHeaderColumnTextColor );
312
314
Rect rect = new Rect ();
313
- mTimeTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
315
+ final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM" ;
316
+ mTimeTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
317
+ mTimeTextWidth = mTimeTextPaint .measureText (exampleTime );
314
318
mTimeTextHeight = rect .height ();
315
319
mHeaderMarginBottom = mTimeTextHeight / 2 ;
316
320
initTextTimeWidth ();
@@ -320,7 +324,7 @@ private void init() {
320
324
mHeaderTextPaint .setColor (mHeaderColumnTextColor );
321
325
mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
322
326
mHeaderTextPaint .setTextSize (mTextSize );
323
- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
327
+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
324
328
mHeaderTextHeight = rect .height ();
325
329
mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
326
330
@@ -410,7 +414,7 @@ private void initTextTimeWidth() {
410
414
mTimeTextWidth = 0 ;
411
415
for (int i = 0 ; i < 24 ; i ++) {
412
416
// measure time string and get max width
413
- String time = getDateTimeInterpreter ().interpretTime (i );
417
+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
414
418
if (time == null )
415
419
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
416
420
mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -438,11 +442,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
438
442
// Draw the background color for the header column.
439
443
canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
440
444
441
- for (int i = 0 ; i < 24 ; i ++) {
442
- float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
445
+ int numPeriodsInDay = showHalfHours ? 48 : 24 ;
446
+ for (int i = 0 ; i < numPeriodsInDay ; i ++) {
447
+ // If we are showing half hours (eg. 5:30am), space the times out by half the hour height
448
+ // and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
449
+ int timeSpacing ;
450
+ int minutes ;
451
+ int hour ;
452
+ if (showHalfHours ) {
453
+ timeSpacing = mHourHeight / 2 ;
454
+ hour = i / 2 ;
455
+ if (i % 2 == 0 ) {
456
+ minutes = 0 ;
457
+ } else {
458
+ minutes = 30 ;
459
+ }
460
+ } else {
461
+ timeSpacing = mHourHeight ;
462
+ hour = i ;
463
+ minutes = 0 ;
464
+ }
465
+
466
+ // Calculate the top of the rectangle where the time text will go
467
+ float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + timeSpacing * i + mHeaderMarginBottom ;
468
+
469
+ // Get the time to be displayed, as a String.
470
+ String time = getDateTimeInterpreter ().interpretTime (hour , minutes );
443
471
444
472
// 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.
445
- String time = getDateTimeInterpreter ().interpretTime (i );
446
473
if (time == null )
447
474
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
448
475
if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1130,13 +1157,22 @@ public String interpretDate(Calendar date) {
1130
1157
}
1131
1158
1132
1159
@ Override
1133
- public String interpretTime (int hour ) {
1160
+ public String interpretTime (int hour , int minutes ) {
1134
1161
Calendar calendar = Calendar .getInstance ();
1135
1162
calendar .set (Calendar .HOUR_OF_DAY , hour );
1136
- calendar .set (Calendar .MINUTE , 0 );
1163
+ calendar .set (Calendar .MINUTE , minutes );
1137
1164
1138
1165
try {
1139
- SimpleDateFormat sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1166
+ SimpleDateFormat sdf ;
1167
+ if (DateFormat .is24HourFormat (getContext ())) {
1168
+ sdf = new SimpleDateFormat ("HH:mm" , Locale .getDefault ());
1169
+ } else {
1170
+ if (showHalfHours ) {
1171
+ sdf = new SimpleDateFormat ("hh:mm a" , Locale .getDefault ());
1172
+ } else {
1173
+ sdf = new SimpleDateFormat ("hh a" , Locale .getDefault ());
1174
+ }
1175
+ }
1140
1176
return sdf .format (calendar .getTime ());
1141
1177
} catch (Exception e ) {
1142
1178
e .printStackTrace ();
0 commit comments