@@ -127,6 +127,7 @@ public class WeekView extends View {
127
127
private boolean mShowDistinctWeekendColor = false ;
128
128
private boolean mShowNowLine = false ;
129
129
private boolean mShowDistinctPastFutureColor = false ;
130
+ private boolean showHalfHours = false ;
130
131
131
132
// Listeners.
132
133
private EventClickListener mEventClickListener ;
@@ -303,7 +304,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
303
304
mShowDistinctPastFutureColor = a .getBoolean (R .styleable .WeekView_showDistinctPastFutureColor , mShowDistinctPastFutureColor );
304
305
mShowDistinctWeekendColor = a .getBoolean (R .styleable .WeekView_showDistinctWeekendColor , mShowDistinctWeekendColor );
305
306
mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
306
-
307
+ showHalfHours = a . getBoolean ( R . styleable . WeekView_showHalfHours , showHalfHours );
307
308
} finally {
308
309
a .recycle ();
309
310
}
@@ -322,7 +323,9 @@ private void init() {
322
323
mTimeTextPaint .setTextSize (mTextSize );
323
324
mTimeTextPaint .setColor (mHeaderColumnTextColor );
324
325
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 );
326
329
mTimeTextHeight = rect .height ();
327
330
mHeaderMarginBottom = mTimeTextHeight / 2 ;
328
331
initTextTimeWidth ();
@@ -332,7 +335,7 @@ private void init() {
332
335
mHeaderTextPaint .setColor (mHeaderColumnTextColor );
333
336
mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
334
337
mHeaderTextPaint .setTextSize (mTextSize );
335
- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
338
+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
336
339
mHeaderTextHeight = rect .height ();
337
340
mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
338
341
@@ -427,7 +430,7 @@ private void initTextTimeWidth() {
427
430
mTimeTextWidth = 0 ;
428
431
for (int i = 0 ; i < 24 ; i ++) {
429
432
// Measure time string and get max width.
430
- String time = getDateTimeInterpreter ().interpretTime (i );
433
+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
431
434
if (time == null )
432
435
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
433
436
mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -455,11 +458,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
455
458
// Draw the background color for the header column.
456
459
canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
457
460
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 );
460
487
461
488
// 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 );
463
489
if (time == null )
464
490
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
465
491
if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1201,13 +1227,22 @@ public String interpretDate(Calendar date) {
1201
1227
}
1202
1228
1203
1229
@ Override
1204
- public String interpretTime (int hour ) {
1230
+ public String interpretTime (int hour , int minutes ) {
1205
1231
Calendar calendar = Calendar .getInstance ();
1206
1232
calendar .set (Calendar .HOUR_OF_DAY , hour );
1207
- calendar .set (Calendar .MINUTE , 0 );
1233
+ calendar .set (Calendar .MINUTE , minutes );
1208
1234
1209
1235
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
+ }
1211
1246
return sdf .format (calendar .getTime ());
1212
1247
} catch (Exception e ) {
1213
1248
e .printStackTrace ();
0 commit comments