@@ -138,6 +138,7 @@ private enum Direction {
138
138
private boolean mShowDistinctPastFutureColor = false ;
139
139
private boolean mHorizontalFlingEnabled = true ;
140
140
private boolean mVerticalFlingEnabled = true ;
141
+ private boolean showHalfHours = false ;
141
142
142
143
// Listeners.
143
144
private EventClickListener mEventClickListener ;
@@ -344,6 +345,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
344
345
mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
345
346
mHorizontalFlingEnabled = a .getBoolean (R .styleable .WeekView_horizontalFlingEnabled , mHorizontalFlingEnabled );
346
347
mVerticalFlingEnabled = a .getBoolean (R .styleable .WeekView_verticalFlingEnabled , mVerticalFlingEnabled );
348
+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
347
349
} finally {
348
350
a .recycle ();
349
351
}
@@ -365,7 +367,9 @@ private void init() {
365
367
mTimeTextPaint .setTextSize (mTextSize );
366
368
mTimeTextPaint .setColor (mHeaderColumnTextColor );
367
369
Rect rect = new Rect ();
368
- mTimeTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
370
+ final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM" ;
371
+ mTimeTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
372
+ mTimeTextWidth = mTimeTextPaint .measureText (exampleTime );
369
373
mTimeTextHeight = rect .height ();
370
374
mHeaderMarginBottom = mTimeTextHeight / 2 ;
371
375
initTextTimeWidth ();
@@ -375,7 +379,7 @@ private void init() {
375
379
mHeaderTextPaint .setColor (mHeaderColumnTextColor );
376
380
mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
377
381
mHeaderTextPaint .setTextSize (mTextSize );
378
- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
382
+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
379
383
mHeaderTextHeight = rect .height ();
380
384
mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
381
385
@@ -470,7 +474,7 @@ private void initTextTimeWidth() {
470
474
mTimeTextWidth = 0 ;
471
475
for (int i = 0 ; i < 24 ; i ++) {
472
476
// Measure time string and get max width.
473
- String time = getDateTimeInterpreter ().interpretTime (i );
477
+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
474
478
if (time == null )
475
479
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
476
480
mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -498,11 +502,35 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
498
502
// Clip to paint in left column only.
499
503
canvas .clipRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), Region .Op .REPLACE );
500
504
501
- for (int i = 0 ; i < 24 ; i ++) {
502
- float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
505
+
506
+ int numPeriodsInDay = showHalfHours ? 48 : 24 ;
507
+ for (int i = 0 ; i < numPeriodsInDay ; i ++) {
508
+ // If we are showing half hours (eg. 5:30am), space the times out by half the hour height
509
+ // and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
510
+ int timeSpacing ;
511
+ int minutes ;
512
+ int hour ;
513
+ if (showHalfHours ) {
514
+ timeSpacing = mHourHeight / 2 ;
515
+ hour = i / 2 ;
516
+ if (i % 2 == 0 ) {
517
+ minutes = 0 ;
518
+ } else {
519
+ minutes = 30 ;
520
+ }
521
+ } else {
522
+ timeSpacing = mHourHeight ;
523
+ hour = i ;
524
+ minutes = 0 ;
525
+ }
526
+
527
+ // Calculate the top of the rectangle where the time text will go
528
+ float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + timeSpacing * i + mHeaderMarginBottom ;
529
+
530
+ // Get the time to be displayed, as a String.
531
+ String time = getDateTimeInterpreter ().interpretTime (hour , minutes );
503
532
504
533
// 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.
505
- String time = getDateTimeInterpreter ().interpretTime (i );
506
534
if (time == null )
507
535
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
508
536
if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1251,13 +1279,22 @@ public String interpretDate(Calendar date) {
1251
1279
}
1252
1280
1253
1281
@ Override
1254
- public String interpretTime (int hour ) {
1282
+ public String interpretTime (int hour , int minutes ) {
1255
1283
Calendar calendar = Calendar .getInstance ();
1256
1284
calendar .set (Calendar .HOUR_OF_DAY , hour );
1257
- calendar .set (Calendar .MINUTE , 0 );
1285
+ calendar .set (Calendar .MINUTE , minutes );
1258
1286
1259
1287
try {
1260
- SimpleDateFormat sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1288
+ SimpleDateFormat sdf ;
1289
+ if (DateFormat .is24HourFormat (getContext ())) {
1290
+ sdf = new SimpleDateFormat ("HH:mm" , Locale .getDefault ());
1291
+ } else {
1292
+ if (showHalfHours ) {
1293
+ sdf = new SimpleDateFormat ("hh:mm a" , Locale .getDefault ());
1294
+ } else {
1295
+ sdf = new SimpleDateFormat ("hh a" , Locale .getDefault ());
1296
+ }
1297
+ }
1261
1298
return sdf .format (calendar .getTime ());
1262
1299
} catch (Exception e ) {
1263
1300
e .printStackTrace ();
0 commit comments