@@ -142,6 +142,7 @@ private enum Direction {
142
142
private boolean mShowDistinctPastFutureColor = false ;
143
143
private boolean mHorizontalFlingEnabled = true ;
144
144
private boolean mVerticalFlingEnabled = true ;
145
+ private boolean showHalfHours = false ;
145
146
private int mAllDayEventHeight = 100 ;
146
147
private int mScrollDuration = 250 ;
147
148
@@ -351,6 +352,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
351
352
mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
352
353
mHorizontalFlingEnabled = a .getBoolean (R .styleable .WeekView_horizontalFlingEnabled , mHorizontalFlingEnabled );
353
354
mVerticalFlingEnabled = a .getBoolean (R .styleable .WeekView_verticalFlingEnabled , mVerticalFlingEnabled );
355
+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
354
356
mAllDayEventHeight = a .getDimensionPixelSize (R .styleable .WeekView_allDayEventHeight , mAllDayEventHeight );
355
357
mScrollDuration = a .getInt (R .styleable .WeekView_scrollDuration , mScrollDuration );
356
358
} finally {
@@ -374,7 +376,9 @@ private void init() {
374
376
mTimeTextPaint .setTextSize (mTextSize );
375
377
mTimeTextPaint .setColor (mHeaderColumnTextColor );
376
378
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 );
378
382
mTimeTextHeight = rect .height ();
379
383
mHeaderMarginBottom = mTimeTextHeight / 2 ;
380
384
initTextTimeWidth ();
@@ -384,7 +388,7 @@ private void init() {
384
388
mHeaderTextPaint .setColor (mHeaderColumnTextColor );
385
389
mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
386
390
mHeaderTextPaint .setTextSize (mTextSize );
387
- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
391
+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
388
392
mHeaderTextHeight = rect .height ();
389
393
mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
390
394
@@ -479,7 +483,7 @@ private void initTextTimeWidth() {
479
483
mTimeTextWidth = 0 ;
480
484
for (int i = 0 ; i < 24 ; i ++) {
481
485
// Measure time string and get max width.
482
- String time = getDateTimeInterpreter ().interpretTime (i );
486
+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
483
487
if (time == null )
484
488
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
485
489
mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -533,11 +537,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
533
537
// Clip to paint in left column only.
534
538
canvas .clipRect (0 , mHeaderHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), Region .Op .REPLACE );
535
539
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 );
538
566
539
567
// 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 );
541
568
if (time == null )
542
569
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
543
570
if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1311,13 +1338,22 @@ public String interpretDate(Calendar date) {
1311
1338
}
1312
1339
1313
1340
@ Override
1314
- public String interpretTime (int hour ) {
1341
+ public String interpretTime (int hour , int minutes ) {
1315
1342
Calendar calendar = Calendar .getInstance ();
1316
1343
calendar .set (Calendar .HOUR_OF_DAY , hour );
1317
- calendar .set (Calendar .MINUTE , 0 );
1344
+ calendar .set (Calendar .MINUTE , minutes );
1318
1345
1319
1346
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
+ }
1321
1357
return sdf .format (calendar .getTime ());
1322
1358
} catch (Exception e ) {
1323
1359
e .printStackTrace ();
0 commit comments