@@ -127,6 +127,7 @@ public class WeekView extends View {
127127 private boolean mShowDistinctWeekendColor = false ;
128128 private boolean mShowNowLine = false ;
129129 private boolean mShowDistinctPastFutureColor = false ;
130+ private boolean showHalfHours = false ;
130131
131132 // Listeners.
132133 private EventClickListener mEventClickListener ;
@@ -303,7 +304,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
303304 mShowDistinctPastFutureColor = a .getBoolean (R .styleable .WeekView_showDistinctPastFutureColor , mShowDistinctPastFutureColor );
304305 mShowDistinctWeekendColor = a .getBoolean (R .styleable .WeekView_showDistinctWeekendColor , mShowDistinctWeekendColor );
305306 mShowNowLine = a .getBoolean (R .styleable .WeekView_showNowLine , mShowNowLine );
306-
307+ showHalfHours = a . getBoolean ( R . styleable . WeekView_showHalfHours , showHalfHours );
307308 } finally {
308309 a .recycle ();
309310 }
@@ -322,7 +323,9 @@ private void init() {
322323 mTimeTextPaint .setTextSize (mTextSize );
323324 mTimeTextPaint .setColor (mHeaderColumnTextColor );
324325 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 );
326329 mTimeTextHeight = rect .height ();
327330 mHeaderMarginBottom = mTimeTextHeight / 2 ;
328331 initTextTimeWidth ();
@@ -332,7 +335,7 @@ private void init() {
332335 mHeaderTextPaint .setColor (mHeaderColumnTextColor );
333336 mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
334337 mHeaderTextPaint .setTextSize (mTextSize );
335- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
338+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
336339 mHeaderTextHeight = rect .height ();
337340 mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
338341
@@ -427,7 +430,7 @@ private void initTextTimeWidth() {
427430 mTimeTextWidth = 0 ;
428431 for (int i = 0 ; i < 24 ; i ++) {
429432 // Measure time string and get max width.
430- String time = getDateTimeInterpreter ().interpretTime (i );
433+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
431434 if (time == null )
432435 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
433436 mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -455,11 +458,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
455458 // Draw the background color for the header column.
456459 canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
457460
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 );
460487
461488 // 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 );
463489 if (time == null )
464490 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
465491 if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1201,13 +1227,22 @@ public String interpretDate(Calendar date) {
12011227 }
12021228
12031229 @ Override
1204- public String interpretTime (int hour ) {
1230+ public String interpretTime (int hour , int minutes ) {
12051231 Calendar calendar = Calendar .getInstance ();
12061232 calendar .set (Calendar .HOUR_OF_DAY , hour );
1207- calendar .set (Calendar .MINUTE , 0 );
1233+ calendar .set (Calendar .MINUTE , minutes );
12081234
12091235 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+ }
12111246 return sdf .format (calendar .getTime ());
12121247 } catch (Exception e ) {
12131248 e .printStackTrace ();
0 commit comments