Advertisement
Guest User

CalendarView.java

a guest
Jul 17th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. public class CalendarView extends Activity {
  2.  
  3. public GregorianCalendar month, itemmonth;// calendar instances.
  4.  
  5. public CalendarAdapter adapter;// adapter instance
  6. public Handler handler;// for grabbing some event values for showing the dot
  7. // marker.
  8. public ArrayList<String> items; // container to store calendar items which
  9. // needs showing the event marker
  10. ArrayList<String> event;
  11. LinearLayout rLayout;
  12. ArrayList<String> date;
  13. ArrayList<String> desc;
  14.  
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.calendar);
  18. Locale.setDefault(Locale.US);
  19.  
  20. rLayout = (LinearLayout) findViewById(R.id.text);
  21. month = (GregorianCalendar) GregorianCalendar.getInstance();
  22. itemmonth = (GregorianCalendar) month.clone();
  23.  
  24. items = new ArrayList<String>();
  25.  
  26. adapter = new CalendarAdapter(this, month);
  27.  
  28. GridView gridview = (GridView) findViewById(R.id.gridview);
  29. gridview.setAdapter(adapter);
  30.  
  31. handler = new Handler();
  32. handler.post(calendarUpdater);
  33.  
  34. TextView title = (TextView) findViewById(R.id.title);
  35. title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
  36.  
  37. RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);
  38.  
  39. previous.setOnClickListener(new OnClickListener() {
  40.  
  41. @Override
  42. public void onClick(View v) {
  43. setPreviousMonth();
  44. refreshCalendar();
  45. }
  46. });
  47.  
  48. RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
  49. next.setOnClickListener(new OnClickListener() {
  50.  
  51. @Override
  52. public void onClick(View v) {
  53. setNextMonth();
  54. refreshCalendar();
  55.  
  56. }
  57. });
  58.  
  59. gridview.setOnItemClickListener(new OnItemClickListener() {
  60. public void onItemClick(AdapterView<?> parent, View v,
  61. int position, long id) {
  62. // removing the previous view if added
  63. if (((LinearLayout) rLayout).getChildCount() > 0) {
  64. ((LinearLayout) rLayout).removeAllViews();
  65. }
  66. desc = new ArrayList<String>();
  67. date = new ArrayList<String>();
  68. ((CalendarAdapter) parent.getAdapter()).setSelected(v);
  69. String selectedGridDate = CalendarAdapter.dayString
  70. .get(position);
  71. String[] separatedTime = selectedGridDate.split("-");
  72. String gridvalueString = separatedTime[2].replaceFirst("^0*",
  73. "");// taking last part of date. ie; 2 from 2012-12-02.
  74. int gridvalue = Integer.parseInt(gridvalueString);
  75. // navigate to next or previous month on clicking offdays.
  76. if ((gridvalue > 10) && (position < 8)) {
  77. setPreviousMonth();
  78. refreshCalendar();
  79. } else if ((gridvalue < 7) && (position > 28)) {
  80. setNextMonth();
  81. refreshCalendar();
  82. }
  83. ((CalendarAdapter) parent.getAdapter()).setSelected(v);
  84.  
  85. for (int i = 0; i < Utility.startDates.size(); i++) {
  86. if (Utility.startDates.get(i).equals(selectedGridDate)) {
  87. desc.add(Utility.nameOfEvent.get(i));
  88. }
  89. }
  90.  
  91. if (desc.size() > 0) {
  92. for (int i = 0; i < desc.size(); i++) {
  93. TextView rowTextView = new TextView(CalendarView.this);
  94.  
  95. // set some properties of rowTextView or something
  96. rowTextView.setText("Event:" + desc.get(i));
  97. rowTextView.setTextColor(Color.BLACK);
  98.  
  99. // add the textview to the linearlayout
  100. rLayout.addView(rowTextView);
  101.  
  102. }
  103.  
  104. }
  105.  
  106. desc = null;
  107.  
  108. }
  109.  
  110. });
  111. }
  112.  
  113. protected void setNextMonth() {
  114. if (month.get(GregorianCalendar.MONTH) == month
  115. .getActualMaximum(GregorianCalendar.MONTH)) {
  116. month.set((month.get(GregorianCalendar.YEAR) + 1),
  117. month.getActualMinimum(GregorianCalendar.MONTH), 1);
  118. } else {
  119. month.set(GregorianCalendar.MONTH,
  120. month.get(GregorianCalendar.MONTH) + 1);
  121. }
  122.  
  123. }
  124.  
  125. protected void setPreviousMonth() {
  126. if (month.get(GregorianCalendar.MONTH) == month
  127. .getActualMinimum(GregorianCalendar.MONTH)) {
  128. month.set((month.get(GregorianCalendar.YEAR) - 1),
  129. month.getActualMaximum(GregorianCalendar.MONTH), 1);
  130. } else {
  131. month.set(GregorianCalendar.MONTH,
  132. month.get(GregorianCalendar.MONTH) - 1);
  133. }
  134.  
  135. }
  136.  
  137. protected void showToast(String string) {
  138. Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
  139.  
  140. }
  141.  
  142. public void refreshCalendar() {
  143. TextView title = (TextView) findViewById(R.id.title);
  144.  
  145. adapter.refreshDays();
  146. adapter.notifyDataSetChanged();
  147. handler.post(calendarUpdater); // generate some calendar items
  148.  
  149. title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
  150. }
  151.  
  152. public Runnable calendarUpdater = new Runnable() {
  153.  
  154. @Override
  155. public void run() {
  156. items.clear();
  157.  
  158. // Print dates of the current week
  159. DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
  160. String itemvalue;
  161. event = Utility.readCalendarEvent(CalendarView.this);
  162. Log.d("=====Event====", event.toString());
  163. Log.d("=====Date ARRAY====", Utility.startDates.toString());
  164.  
  165. for (int i = 0; i < Utility.startDates.size(); i++) {
  166. itemvalue = df.format(itemmonth.getTime());
  167. itemmonth.add(GregorianCalendar.DATE, 1);
  168. items.add(Utility.startDates.get(i).toString());
  169. }
  170. adapter.setItems(items);
  171. adapter.notifyDataSetChanged();
  172. }
  173. };
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement