Skip to content

Commit de905c3

Browse files
committed
Show short dates in 7 day view in the sample app
1 parent 06ed11f commit de905c3

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
37-
* Website: http://alamkanak.me
37+
* Website: http://alamkanak.github.io/
3838
*/
3939
public class WeekView extends View {
4040

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
import android.view.MenuItem;
99
import android.widget.Toast;
1010

11+
import com.alamkanak.weekview.DateTimeInterpreter;
1112
import com.alamkanak.weekview.WeekView;
1213
import com.alamkanak.weekview.WeekViewEvent;
1314

15+
import java.text.SimpleDateFormat;
1416
import java.util.ArrayList;
1517
import java.util.Calendar;
1618
import java.util.List;
19+
import java.util.Locale;
1720

1821

1922
/**
2023
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
21-
* Website: http://alamkanak.github.io
24+
* Website: http://alamkanak.github.io/
2225
*/
2326
public class MainActivity extends ActionBarActivity implements WeekView.MonthChangeListener,
2427
WeekView.EventClickListener, WeekView.EventLongPressListener {
@@ -58,6 +61,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
5861
@Override
5962
public boolean onOptionsItemSelected(MenuItem item) {
6063
int id = item.getItemId();
64+
setupDateTimeInterpreter(id == R.id.action_week_view);
6165
switch (id){
6266
case R.id.action_today:
6367
mWeekView.goToToday();
@@ -103,6 +107,34 @@ public boolean onOptionsItemSelected(MenuItem item) {
103107
return super.onOptionsItemSelected(item);
104108
}
105109

110+
/**
111+
* Set up a date time interpreter which will show short date values when in week view and long
112+
* date values otherwise.
113+
* @param shortDate True if the date values should be short.
114+
*/
115+
private void setupDateTimeInterpreter(final boolean shortDate) {
116+
mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() {
117+
@Override
118+
public String interpretDate(Calendar date) {
119+
SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault());
120+
String weekday = weekdayNameFormat.format(date.getTime());
121+
SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault());
122+
123+
// All android api level do not have a standard way of getting the first letter of
124+
// the week day name. Hence we get the first char programmatically.
125+
// Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657
126+
if (shortDate)
127+
weekday = String.valueOf(weekday.charAt(0));
128+
return weekday + format.format(date.getTime());
129+
}
130+
131+
@Override
132+
public String interpretTime(int hour) {
133+
return hour > 11 ? (hour - 12) + "PM" : hour + "AM";
134+
}
135+
});
136+
}
137+
106138
@Override
107139
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
108140

@@ -210,8 +242,6 @@ public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
210242
return events;
211243
}
212244

213-
214-
215245
private String getEventTitle(Calendar time) {
216246
return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH)+1, time.get(Calendar.DAY_OF_MONTH));
217247
}

0 commit comments

Comments
 (0)