Skip to content

Commit 2c65d3e

Browse files
committed
show half hour marks on the left-hand column
1 parent c1f7d0b commit 2c65d3e

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
public interface DateTimeInterpreter {
99
String interpretDate(Calendar date);
10-
String interpretTime(int hour);
10+
String interpretTime(int hour, int minutes);
1111
}

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

library/src/main/res/values/attrs.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
<attr name="eventMarginVertical" format="dimension"/>
4545
<attr name="xScrollingSpeed" format="float"/>
4646
<attr name="eventCornerRadius" format="dimension"/>
47+
<attr name="showHalfHours" format="boolean" />
4748
</declare-styleable>
48-
</resources>
49+
</resources>

sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.RectF;
44
import android.os.Bundle;
55
import android.support.v7.app.ActionBarActivity;
6+
import android.util.Log;
67
import android.util.TypedValue;
78
import android.view.Menu;
89
import android.view.MenuItem;
@@ -12,6 +13,7 @@
1213
import com.alamkanak.weekview.WeekView;
1314
import com.alamkanak.weekview.WeekViewEvent;
1415

16+
import java.text.Format;
1517
import java.text.SimpleDateFormat;
1618
import java.util.ArrayList;
1719
import java.util.Calendar;
@@ -133,8 +135,18 @@ public String interpretDate(Calendar date) {
133135
}
134136

135137
@Override
136-
public String interpretTime(int hour) {
137-
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
138+
public String interpretTime(int hour, int minutes) {
139+
String strMinutes = String.format("%02d", minutes);
140+
if (hour > 11) {
141+
return (hour - 12) + ":" + strMinutes + " PM";
142+
}
143+
else {
144+
if (hour == 0) {
145+
return "12:" + strMinutes + " AM";
146+
} else {
147+
return hour + ":" + strMinutes + " AM";
148+
}
149+
}
138150
}
139151
});
140152
}
@@ -243,6 +255,19 @@ public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
243255
event.setColor(getResources().getColor(R.color.event_color_02));
244256
events.add(event);
245257

258+
startTime = Calendar.getInstance();
259+
startTime.set(Calendar.HOUR_OF_DAY, 18);
260+
startTime.set(Calendar.MINUTE, 30);
261+
startTime.set(Calendar.MONTH, newMonth-1);
262+
startTime.set(Calendar.YEAR, newYear);
263+
endTime = (Calendar) startTime.clone();
264+
endTime.set(Calendar.HOUR_OF_DAY, 19);
265+
endTime.set(Calendar.MINUTE, 30);
266+
endTime.set(Calendar.MONTH, newMonth-1);
267+
event = new WeekViewEvent(22, getEventTitle(startTime), startTime, endTime);
268+
event.setColor(getResources().getColor(R.color.event_color_02));
269+
events.add(event);
270+
246271
return events;
247272
}
248273

sample/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
app:dayBackgroundColor="#05000000"
2323
app:todayBackgroundColor="#1848adff"
2424
app:headerColumnBackground="#ffffffff"
25-
app:todayHeaderTextColor="@color/accent"/>
25+
app:todayHeaderTextColor="@color/accent"
26+
app:showHalfHours="false"/>
2627

2728
</RelativeLayout>

0 commit comments

Comments
 (0)