Skip to content

Commit 90c3663

Browse files
committed
show half hour marks on the left-hand column
1 parent cc34a54 commit 90c3663

File tree

7 files changed

+74
-15
lines changed

7 files changed

+74
-15
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: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private enum Direction {
138138
private boolean mShowDistinctPastFutureColor = false;
139139
private boolean mHorizontalFlingEnabled = true;
140140
private boolean mVerticalFlingEnabled = true;
141+
private boolean showHalfHours = false;
141142

142143
// Listeners.
143144
private EventClickListener mEventClickListener;
@@ -344,6 +345,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
344345
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);
345346
mHorizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, mHorizontalFlingEnabled);
346347
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
348+
showHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, showHalfHours);
347349
} finally {
348350
a.recycle();
349351
}
@@ -365,7 +367,9 @@ private void init() {
365367
mTimeTextPaint.setTextSize(mTextSize);
366368
mTimeTextPaint.setColor(mHeaderColumnTextColor);
367369
Rect rect = new Rect();
368-
mTimeTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
370+
final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM";
371+
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
372+
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
369373
mTimeTextHeight = rect.height();
370374
mHeaderMarginBottom = mTimeTextHeight / 2;
371375
initTextTimeWidth();
@@ -375,7 +379,7 @@ private void init() {
375379
mHeaderTextPaint.setColor(mHeaderColumnTextColor);
376380
mHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
377381
mHeaderTextPaint.setTextSize(mTextSize);
378-
mHeaderTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
382+
mHeaderTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
379383
mHeaderTextHeight = rect.height();
380384
mHeaderTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
381385

@@ -470,7 +474,7 @@ private void initTextTimeWidth() {
470474
mTimeTextWidth = 0;
471475
for (int i = 0; i < 24; i++) {
472476
// Measure time string and get max width.
473-
String time = getDateTimeInterpreter().interpretTime(i);
477+
String time = getDateTimeInterpreter().interpretTime(i, 0);
474478
if (time == null)
475479
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
476480
mTimeTextWidth = Math.max(mTimeTextWidth, mTimeTextPaint.measureText(time));
@@ -498,11 +502,35 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
498502
// Clip to paint in left column only.
499503
canvas.clipRect(0, mHeaderTextHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE);
500504

501-
for (int i = 0; i < 24; i++) {
502-
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
505+
506+
int numPeriodsInDay = showHalfHours ? 48 : 24;
507+
for (int i = 0; i < numPeriodsInDay; i++) {
508+
// If we are showing half hours (eg. 5:30am), space the times out by half the hour height
509+
// and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
510+
int timeSpacing;
511+
int minutes;
512+
int hour;
513+
if (showHalfHours) {
514+
timeSpacing = mHourHeight / 2;
515+
hour = i / 2;
516+
if (i % 2 == 0) {
517+
minutes = 0;
518+
} else {
519+
minutes = 30;
520+
}
521+
} else {
522+
timeSpacing = mHourHeight;
523+
hour = i;
524+
minutes = 0;
525+
}
526+
527+
// Calculate the top of the rectangle where the time text will go
528+
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + timeSpacing * i + mHeaderMarginBottom;
529+
530+
// Get the time to be displayed, as a String.
531+
String time = getDateTimeInterpreter().interpretTime(hour, minutes);
503532

504533
// 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.
505-
String time = getDateTimeInterpreter().interpretTime(i);
506534
if (time == null)
507535
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
508536
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
@@ -1251,13 +1279,22 @@ public String interpretDate(Calendar date) {
12511279
}
12521280

12531281
@Override
1254-
public String interpretTime(int hour) {
1282+
public String interpretTime(int hour, int minutes) {
12551283
Calendar calendar = Calendar.getInstance();
12561284
calendar.set(Calendar.HOUR_OF_DAY, hour);
1257-
calendar.set(Calendar.MINUTE, 0);
1285+
calendar.set(Calendar.MINUTE, minutes);
12581286

12591287
try {
1260-
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
1288+
SimpleDateFormat sdf;
1289+
if (DateFormat.is24HourFormat(getContext())) {
1290+
sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
1291+
} else {
1292+
if (showHalfHours) {
1293+
sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
1294+
} else {
1295+
sdf = new SimpleDateFormat("hh a", Locale.getDefault());
1296+
}
1297+
}
12611298
return sdf.format(calendar.getTime());
12621299
} catch (Exception e) {
12631300
e.printStackTrace();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
<attr name="nowLineThickness" format="dimension"/>
4949
<attr name="horizontalFlingEnabled" format="boolean"/>
5050
<attr name="verticalFlingEnabled" format="boolean"/>
51+
<attr name="showHalfHours" format="boolean" />
5152
</declare-styleable>
5253
</resources>

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,17 @@ public String interpretDate(Calendar date) {
135135
}
136136

137137
@Override
138-
public String interpretTime(int hour) {
139-
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+
} else {
143+
if (hour == 0) {
144+
return "12:" + strMinutes + " AM";
145+
} else {
146+
return hour + ":" + strMinutes + " AM";
147+
}
148+
}
140149
}
141150
});
142151
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
116116
event.setColor(getResources().getColor(R.color.event_color_02));
117117
events.add(event);
118118

119+
startTime = Calendar.getInstance();
120+
startTime.set(Calendar.HOUR_OF_DAY, 18);
121+
startTime.set(Calendar.MINUTE, 30);
122+
startTime.set(Calendar.MONTH, newMonth-1);
123+
startTime.set(Calendar.YEAR, newYear);
124+
endTime = (Calendar) startTime.clone();
125+
endTime.set(Calendar.HOUR_OF_DAY, 19);
126+
endTime.set(Calendar.MINUTE, 30);
127+
endTime.set(Calendar.MONTH, newMonth-1);
128+
event = new WeekViewEvent(22, getEventTitle(startTime), startTime, endTime);
129+
event.setColor(getResources().getColor(R.color.event_color_02));
130+
events.add(event);
131+
119132
return events;
120133
}
121134

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.view.View;
77

8-
98
/**
109
* The launcher activity of the sample app. It contains the links to visit all the example screens.
1110
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
@@ -34,5 +33,4 @@ public void onClick(View v) {
3433
}
3534
});
3635
}
37-
3836
}

sample/src/main/res/layout/activity_base.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)