|
8 | 8 | import android.view.MenuItem;
|
9 | 9 | import android.widget.Toast;
|
10 | 10 |
|
| 11 | +import com.alamkanak.weekview.DateTimeInterpreter; |
11 | 12 | import com.alamkanak.weekview.WeekView;
|
12 | 13 | import com.alamkanak.weekview.WeekViewEvent;
|
13 | 14 |
|
| 15 | +import java.text.SimpleDateFormat; |
14 | 16 | import java.util.ArrayList;
|
15 | 17 | import java.util.Calendar;
|
16 | 18 | import java.util.List;
|
| 19 | +import java.util.Locale; |
17 | 20 |
|
18 | 21 |
|
19 | 22 | /**
|
20 | 23 | * Created by Raquib-ul-Alam Kanak on 7/21/2014.
|
21 |
| - * Website: http://alamkanak.github.io |
| 24 | + * Website: http://alamkanak.github.io/ |
22 | 25 | */
|
23 | 26 | public class MainActivity extends ActionBarActivity implements WeekView.MonthChangeListener,
|
24 | 27 | WeekView.EventClickListener, WeekView.EventLongPressListener {
|
@@ -58,6 +61,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
|
58 | 61 | @Override
|
59 | 62 | public boolean onOptionsItemSelected(MenuItem item) {
|
60 | 63 | int id = item.getItemId();
|
| 64 | + setupDateTimeInterpreter(id == R.id.action_week_view); |
61 | 65 | switch (id){
|
62 | 66 | case R.id.action_today:
|
63 | 67 | mWeekView.goToToday();
|
@@ -103,6 +107,34 @@ public boolean onOptionsItemSelected(MenuItem item) {
|
103 | 107 | return super.onOptionsItemSelected(item);
|
104 | 108 | }
|
105 | 109 |
|
| 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 | + |
106 | 138 | @Override
|
107 | 139 | public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
|
108 | 140 |
|
@@ -210,8 +242,6 @@ public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
|
210 | 242 | return events;
|
211 | 243 | }
|
212 | 244 |
|
213 |
| - |
214 |
| - |
215 | 245 | private String getEventTitle(Calendar time) {
|
216 | 246 | 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));
|
217 | 247 | }
|
|
0 commit comments