1919import android .text .format .DateFormat ;
2020import android .text .style .StyleSpan ;
2121import android .util .AttributeSet ;
22+ import android .util .Log ;
2223import android .util .TypedValue ;
2324import android .view .GestureDetector ;
2425import android .view .HapticFeedbackConstants ;
@@ -128,6 +129,7 @@ public class WeekView extends View {
128129 private ScaleGestureDetector mScaleDetector ;
129130 private boolean mIsZooming ;
130131 private int mEventCornerRadius = 0 ;
132+ private boolean showHalfHours = false ;
131133
132134 // Listeners.
133135 private EventClickListener mEventClickListener ;
@@ -291,6 +293,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
291293 mEventMarginVertical = a .getDimensionPixelSize (R .styleable .WeekView_eventMarginVertical , mEventMarginVertical );
292294 mXScrollingSpeed = a .getFloat (R .styleable .WeekView_xScrollingSpeed , mXScrollingSpeed );
293295 mEventCornerRadius = a .getDimensionPixelSize (R .styleable .WeekView_eventCornerRadius , mEventCornerRadius );
296+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
294297 } finally {
295298 a .recycle ();
296299 }
@@ -310,7 +313,9 @@ private void init() {
310313 mTimeTextPaint .setTextSize (mTextSize );
311314 mTimeTextPaint .setColor (mHeaderColumnTextColor );
312315 Rect rect = new Rect ();
313- mTimeTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
316+ final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM" ;
317+ mTimeTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
318+ mTimeTextWidth = mTimeTextPaint .measureText (exampleTime );
314319 mTimeTextHeight = rect .height ();
315320 mHeaderMarginBottom = mTimeTextHeight / 2 ;
316321 initTextTimeWidth ();
@@ -320,7 +325,7 @@ private void init() {
320325 mHeaderTextPaint .setColor (mHeaderColumnTextColor );
321326 mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
322327 mHeaderTextPaint .setTextSize (mTextSize );
323- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
328+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
324329 mHeaderTextHeight = rect .height ();
325330 mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
326331
@@ -410,7 +415,7 @@ private void initTextTimeWidth() {
410415 mTimeTextWidth = 0 ;
411416 for (int i = 0 ; i < 24 ; i ++) {
412417 // measure time string and get max width
413- String time = getDateTimeInterpreter ().interpretTime (i );
418+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
414419 if (time == null )
415420 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
416421 mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -438,11 +443,35 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
438443 // Draw the background color for the header column.
439444 canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
440445
441- for (int i = 0 ; i < 24 ; i ++) {
442- float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
446+ int numPeriodsInDay = showHalfHours ? 48 : 24 ;
447+ for (int i = 0 ; i < numPeriodsInDay ; i ++) {
448+ // If we are showing half hours (eg. 5:30am), space the times out by half the hour height
449+ // and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
450+ int timeSpacing ;
451+ int minutes ;
452+ int hour ;
453+ if (showHalfHours ) {
454+ timeSpacing = mHourHeight / 2 ;
455+ hour = i / 2 ;
456+ if (i % 2 == 0 ) {
457+ minutes = 0 ;
458+ }
459+ else {
460+ minutes = 30 ;
461+ }
462+ } else {
463+ timeSpacing = mHourHeight ;
464+ hour = i ;
465+ minutes = 0 ;
466+ }
467+
468+ // Calculate the top of the rectangle where the time text will go
469+ float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + timeSpacing * i + mHeaderMarginBottom ;
470+
471+ // Get the time to be displayed, as a String.
472+ String time = getDateTimeInterpreter ().interpretTime (hour , minutes );
443473
444474 // 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 );
446475 if (time == null )
447476 throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
448477 if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1130,13 +1159,18 @@ public String interpretDate(Calendar date) {
11301159 }
11311160
11321161 @ Override
1133- public String interpretTime (int hour ) {
1162+ public String interpretTime (int hour , int minutes ) {
11341163 Calendar calendar = Calendar .getInstance ();
11351164 calendar .set (Calendar .HOUR_OF_DAY , hour );
1136- calendar .set (Calendar .MINUTE , 0 );
1165+ calendar .set (Calendar .MINUTE , minutes );
11371166
11381167 try {
1139- SimpleDateFormat sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1168+ SimpleDateFormat sdf ;
1169+ if (showHalfHours ) {
1170+ sdf = new SimpleDateFormat ("HH:mm a" , Locale .getDefault ());
1171+ } else {
1172+ sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1173+ }
11401174 return sdf .format (calendar .getTime ());
11411175 } catch (Exception e ) {
11421176 e .printStackTrace ();
0 commit comments