Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. @SuppressLint("SimpleDateFormat")
  2. public class CaldroidSampleActivity extends AppCompatActivity {
  3. private boolean undo = false;
  4. private CaldroidFragment caldroidFragment;
  5. private CaldroidFragment dialogCaldroidFragment;
  6.  
  7. private void setCustomResourceForDates() {
  8. Calendar cal = Calendar.getInstance();
  9.  
  10. // Min date is last 7 days
  11. cal.add(Calendar.DATE, -7);
  12. Date blueDate = cal.getTime();
  13.  
  14. // Max date is next 7 days
  15. cal = Calendar.getInstance();
  16. cal.add(Calendar.DATE, 7);
  17. Date greenDate = cal.getTime();
  18.  
  19. if (caldroidFragment != null) {
  20. ColorDrawable blue = new ColorDrawable(getResources().getColor(R.color.blue));
  21. ColorDrawable green = new ColorDrawable(Color.GREEN);
  22. caldroidFragment.setBackgroundDrawableForDate(blue, blueDate);
  23. caldroidFragment.setBackgroundDrawableForDate(green, greenDate);
  24. caldroidFragment.setTextColorForDate(R.color.white, blueDate);
  25. caldroidFragment.setTextColorForDate(R.color.white, greenDate);
  26. }
  27. }
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33.  
  34. final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
  35.  
  36. caldroidFragment = new CaldroidFragment();
  37.  
  38. if (savedInstanceState != null) {
  39. caldroidFragment.restoreStatesFromKey(savedInstanceState,
  40. "CALDROID_SAVED_STATE");
  41. }
  42.  
  43. else {
  44. Bundle args = new Bundle();
  45. Calendar cal = Calendar.getInstance();
  46. args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
  47. args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
  48. args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
  49. args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
  50.  
  51. caldroidFragment.setArguments(args);
  52. }
  53.  
  54. setCustomResourceForDates();
  55.  
  56. // Attach to the activity
  57. FragmentTransaction t = getSupportFragmentManager().beginTransaction();
  58. t.replace(R.id.calendar1, caldroidFragment);
  59. t.commit();
  60.  
  61. // Setup listener
  62. final CaldroidListener listener = new CaldroidListener() {
  63.  
  64. @Override
  65. public void onSelectDate(Date date, View view) {
  66. Toast.makeText(getApplicationContext(), formatter.format(date),
  67. Toast.LENGTH_SHORT).show();
  68. }
  69.  
  70. @Override
  71. public void onChangeMonth(int month, int year) {
  72. String text = "month: " + month + " year: " + year;
  73. Toast.makeText(getApplicationContext(), text,
  74. Toast.LENGTH_SHORT).show();
  75. }
  76.  
  77. @Override
  78. public void onLongClickDate(Date date, View view) {
  79. Toast.makeText(getApplicationContext(),
  80. "Long click " + formatter.format(date),
  81. Toast.LENGTH_SHORT).show();
  82. }
  83.  
  84. @Override
  85. public void onCaldroidViewCreated() {
  86. if (caldroidFragment.getLeftArrowButton() != null) {
  87. Toast.makeText(getApplicationContext(),
  88. "Caldroid view is created", Toast.LENGTH_SHORT)
  89. .show();
  90. }
  91. }
  92.  
  93. };
  94.  
  95. // Setup Caldroid
  96. caldroidFragment.setCaldroidListener(listener);
  97.  
  98. final TextView textView = (TextView) findViewById(R.id.textview);
  99.  
  100. }
  101.  
  102. @Override
  103. protected void onSaveInstanceState(Bundle outState) {
  104. // TODO Auto-generated method stub
  105. super.onSaveInstanceState(outState);
  106.  
  107. if (caldroidFragment != null) {
  108. caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
  109. }
  110.  
  111. if (dialogCaldroidFragment != null) {
  112. dialogCaldroidFragment.saveStatesToKey(outState,
  113. "DIALOG_CALDROID_SAVED_STATE");
  114. }
  115. }
  116.  
  117. }