@@ -138,6 +138,7 @@ private enum Direction {
138138 private boolean mShowDistinctPastFutureColor = false ;
139139 private boolean mHorizontalFlingEnabled = true ;
140140 private boolean mVerticalFlingEnabled = true ;
141+ private boolean showHalfHours = false ;
141142
142143 // Listeners.
143144 private EventClickListener mEventClickListener ;
@@ -344,6 +345,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
344345 mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
345346 mHorizontalFlingEnabled = a .getBoolean (R .styleable .WeekView_horizontalFlingEnabled , mHorizontalFlingEnabled );
346347 mVerticalFlingEnabled = a .getBoolean (R .styleable .WeekView_verticalFlingEnabled , mVerticalFlingEnabled );
348+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
347349 } finally {
348350 a .recycle ();
349351 }
@@ -365,7 +367,9 @@ private void init() {
365367 mTimeTextPaint .setTextSize (mTextSize );
366368 mTimeTextPaint .setColor (mHeaderColumnTextColor );
367369 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 );
369373 mTimeTextHeight = rect .height ();
370374 mHeaderMarginBottom = mTimeTextHeight / 2 ;
371375 initTextTimeWidth ();
@@ -375,7 +379,7 @@ private void init() {
375379 mHeaderTextPaint .setColor (mHeaderColumnTextColor );
376380 mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
377381 mHeaderTextPaint .setTextSize (mTextSize );
378- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
382+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
379383 mHeaderTextHeight = rect .height ();
380384 mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
381385
@@ -470,7 +474,7 @@ private void initTextTimeWidth() {
470474 mTimeTextWidth = 0 ;
471475 for (int i = 0 ; i < 24 ; i ++) {
472476 // Measure time string and get max width.
473- String time = getDateTimeInterpreter ().interpretTime (i );
477+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
474478 if (time == null )
475479 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
476480 mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -498,11 +502,35 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
498502 // Clip to paint in left column only.
499503 canvas .clipRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), Region .Op .REPLACE );
500504
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 );
503532
504533 // 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 );
506534 if (time == null )
507535 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
508536 if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1251,13 +1279,22 @@ public String interpretDate(Calendar date) {
12511279 }
12521280
12531281 @ Override
1254- public String interpretTime (int hour ) {
1282+ public String interpretTime (int hour , int minutes ) {
12551283 Calendar calendar = Calendar .getInstance ();
12561284 calendar .set (Calendar .HOUR_OF_DAY , hour );
1257- calendar .set (Calendar .MINUTE , 0 );
1285+ calendar .set (Calendar .MINUTE , minutes );
12581286
12591287 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+ }
12611298 return sdf .format (calendar .getTime ());
12621299 } catch (Exception e ) {
12631300 e .printStackTrace ();
0 commit comments