@@ -142,6 +142,7 @@ private enum Direction {
142142 private boolean mShowDistinctPastFutureColor = false ;
143143 private boolean mHorizontalFlingEnabled = true ;
144144 private boolean mVerticalFlingEnabled = true ;
145+ private boolean showHalfHours = false ;
145146 private int mAllDayEventHeight = 100 ;
146147 private int mScrollDuration = 250 ;
147148
@@ -351,6 +352,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
351352 mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
352353 mHorizontalFlingEnabled = a .getBoolean (R .styleable .WeekView_horizontalFlingEnabled , mHorizontalFlingEnabled );
353354 mVerticalFlingEnabled = a .getBoolean (R .styleable .WeekView_verticalFlingEnabled , mVerticalFlingEnabled );
355+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
354356 mAllDayEventHeight = a .getDimensionPixelSize (R .styleable .WeekView_allDayEventHeight , mAllDayEventHeight );
355357 mScrollDuration = a .getInt (R .styleable .WeekView_scrollDuration , mScrollDuration );
356358 } finally {
@@ -374,7 +376,9 @@ private void init() {
374376 mTimeTextPaint .setTextSize (mTextSize );
375377 mTimeTextPaint .setColor (mHeaderColumnTextColor );
376378 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 );
378382 mTimeTextHeight = rect .height ();
379383 mHeaderMarginBottom = mTimeTextHeight / 2 ;
380384 initTextTimeWidth ();
@@ -384,7 +388,7 @@ private void init() {
384388 mHeaderTextPaint .setColor (mHeaderColumnTextColor );
385389 mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
386390 mHeaderTextPaint .setTextSize (mTextSize );
387- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
391+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
388392 mHeaderTextHeight = rect .height ();
389393 mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
390394
@@ -479,7 +483,7 @@ private void initTextTimeWidth() {
479483 mTimeTextWidth = 0 ;
480484 for (int i = 0 ; i < 24 ; i ++) {
481485 // Measure time string and get max width.
482- String time = getDateTimeInterpreter ().interpretTime (i );
486+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
483487 if (time == null )
484488 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
485489 mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -533,11 +537,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
533537 // Clip to paint in left column only.
534538 canvas .clipRect (0 , mHeaderHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), Region .Op .REPLACE );
535539
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 );
538566
539567 // 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 );
541568 if (time == null )
542569 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
543570 if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1311,13 +1338,22 @@ public String interpretDate(Calendar date) {
13111338 }
13121339
13131340 @ Override
1314- public String interpretTime (int hour ) {
1341+ public String interpretTime (int hour , int minutes ) {
13151342 Calendar calendar = Calendar .getInstance ();
13161343 calendar .set (Calendar .HOUR_OF_DAY , hour );
1317- calendar .set (Calendar .MINUTE , 0 );
1344+ calendar .set (Calendar .MINUTE , minutes );
13181345
13191346 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+ }
13211357 return sdf .format (calendar .getTime ());
13221358 } catch (Exception e ) {
13231359 e .printStackTrace ();
0 commit comments