@@ -128,6 +128,7 @@ public class WeekView extends View {
128128 private ScaleGestureDetector mScaleDetector ;
129129 private boolean mIsZooming ;
130130 private int mEventCornerRadius = 0 ;
131+ private boolean showHalfHours = false ;
131132
132133 // Listeners.
133134 private EventClickListener mEventClickListener ;
@@ -291,6 +292,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
291292 mEventMarginVertical = a .getDimensionPixelSize (R .styleable .WeekView_eventMarginVertical , mEventMarginVertical );
292293 mXScrollingSpeed = a .getFloat (R .styleable .WeekView_xScrollingSpeed , mXScrollingSpeed );
293294 mEventCornerRadius = a .getDimensionPixelSize (R .styleable .WeekView_eventCornerRadius , mEventCornerRadius );
295+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
294296 } finally {
295297 a .recycle ();
296298 }
@@ -310,7 +312,9 @@ private void init() {
310312 mTimeTextPaint .setTextSize (mTextSize );
311313 mTimeTextPaint .setColor (mHeaderColumnTextColor );
312314 Rect rect = new Rect ();
313- mTimeTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
315+ final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM" ;
316+ mTimeTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
317+ mTimeTextWidth = mTimeTextPaint .measureText (exampleTime );
314318 mTimeTextHeight = rect .height ();
315319 mHeaderMarginBottom = mTimeTextHeight / 2 ;
316320 initTextTimeWidth ();
@@ -320,7 +324,7 @@ private void init() {
320324 mHeaderTextPaint .setColor (mHeaderColumnTextColor );
321325 mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
322326 mHeaderTextPaint .setTextSize (mTextSize );
323- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
327+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
324328 mHeaderTextHeight = rect .height ();
325329 mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
326330
@@ -410,7 +414,7 @@ private void initTextTimeWidth() {
410414 mTimeTextWidth = 0 ;
411415 for (int i = 0 ; i < 24 ; i ++) {
412416 // measure time string and get max width
413- String time = getDateTimeInterpreter ().interpretTime (i );
417+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
414418 if (time == null )
415419 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
416420 mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -438,11 +442,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
438442 // Draw the background color for the header column.
439443 canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
440444
441- for (int i = 0 ; i < 24 ; i ++) {
442- float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
445+ int numPeriodsInDay = showHalfHours ? 48 : 24 ;
446+ for (int i = 0 ; i < numPeriodsInDay ; i ++) {
447+ // If we are showing half hours (eg. 5:30am), space the times out by half the hour height
448+ // and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
449+ int timeSpacing ;
450+ int minutes ;
451+ int hour ;
452+ if (showHalfHours ) {
453+ timeSpacing = mHourHeight / 2 ;
454+ hour = i / 2 ;
455+ if (i % 2 == 0 ) {
456+ minutes = 0 ;
457+ } else {
458+ minutes = 30 ;
459+ }
460+ } else {
461+ timeSpacing = mHourHeight ;
462+ hour = i ;
463+ minutes = 0 ;
464+ }
465+
466+ // Calculate the top of the rectangle where the time text will go
467+ float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + timeSpacing * i + mHeaderMarginBottom ;
468+
469+ // Get the time to be displayed, as a String.
470+ String time = getDateTimeInterpreter ().interpretTime (hour , minutes );
443471
444472 // 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.
445- String time = getDateTimeInterpreter ().interpretTime (i );
446473 if (time == null )
447474 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
448475 if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1130,13 +1157,22 @@ public String interpretDate(Calendar date) {
11301157 }
11311158
11321159 @ Override
1133- public String interpretTime (int hour ) {
1160+ public String interpretTime (int hour , int minutes ) {
11341161 Calendar calendar = Calendar .getInstance ();
11351162 calendar .set (Calendar .HOUR_OF_DAY , hour );
1136- calendar .set (Calendar .MINUTE , 0 );
1163+ calendar .set (Calendar .MINUTE , minutes );
11371164
11381165 try {
1139- SimpleDateFormat sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1166+ SimpleDateFormat sdf ;
1167+ if (DateFormat .is24HourFormat (getContext ())) {
1168+ sdf = new SimpleDateFormat ("HH:mm" , Locale .getDefault ());
1169+ } else {
1170+ if (showHalfHours ) {
1171+ sdf = new SimpleDateFormat ("hh:mm a" , Locale .getDefault ());
1172+ } else {
1173+ sdf = new SimpleDateFormat ("hh a" , Locale .getDefault ());
1174+ }
1175+ }
11401176 return sdf .format (calendar .getTime ());
11411177 } catch (Exception e ) {
11421178 e .printStackTrace ();
0 commit comments