Android Open Source - CalendarAdapter Main Activity






From Project

Back to project page CalendarAdapter.

License

The source code is released under:

MIT License

If you think the Android project CalendarAdapter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.avocado.android.calendaradapter.app;
//ww  w  .  jav a  2  s  .  com
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.avocado.android.calendaradapter.library.CalendarAdapter;


public class MainActivity extends Activity implements CalendarAdapter.OnDateSelectedListener {

    private CalendarAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView calendarListView = (ListView) findViewById(R.id.calendar_list);

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.add(Calendar.MONTH, -1);
        Date startDate = startCalendar.getTime();

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.add(Calendar.MONTH, 3);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endDate = endCalendar.getTime();

        Typeface titleTypeface = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Thin.ttf");

        Typeface daysOfWeekTypeface = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Light.ttf");

        Typeface calendarCellTypeface = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Light.ttf");

        int mainTextColor = getResources().getColor(R.color.main_text);
        int eventColor = getResources().getColor(R.color.event_color);
        int pastFutureCalendarCellBackgroundColor = getResources().getColor(R.color.past_future_background);
        int pastFutureCalendarCellTextColor = getResources().getColor(R.color.past_future_text);
        int pastFutureEventColor = getResources().getColor(R.color.past_future_event);
        int calendarCellBorderColor = getResources().getColor(R.color.calendar_cell_border);
        int todayBackgroundColor = getResources().getColor(R.color.today_background);

        List<TestEvent> testEvents = new ArrayList<TestEvent>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(Calendar.MONTH, -1);
        while (cal.getTime().before(endDate)) {
            // Add a 4 day event
            if (cal.get(Calendar.DAY_OF_MONTH) == 30) {
                Calendar endCal = Calendar.getInstance();
                endCal.setTime(cal.getTime());
                endCal.add(Calendar.DAY_OF_MONTH, 7);
                Date eventEndTime = endCal.getTime();
                testEvents.add(new TestEvent(cal.getTime(), eventEndTime));
            }

            // Add a bunch of 1 day events
            if (cal.get(Calendar.DAY_OF_MONTH) % 5 == 0) {
                testEvents.add(new TestEvent(cal.getTime(), null));
            }

            cal.add(Calendar.DAY_OF_MONTH, 1);
        }

        adapter = new CalendarAdapter.Builder(this)
                .titleTypeface(titleTypeface)
                .titleTextColor(mainTextColor)
                .daysOfWeekTypeface(daysOfWeekTypeface)
                .daysOfWeekTextColor(mainTextColor)
                .calendarCellTypeface(calendarCellTypeface)
                .calendarCellTextColor(mainTextColor)
                .startDate(startDate)
                .endDate(endDate)
                .eventColor(eventColor)
                .pastFutureCalendarCellBackgroundColor(pastFutureCalendarCellBackgroundColor)
                .pastFutureCalendarCellTextColor(pastFutureCalendarCellTextColor)
                .pastFutureEventColor(pastFutureEventColor)
                .calendarCellBorderColor(calendarCellBorderColor)
                .todayCalendarCellBackgroundColor(todayBackgroundColor)
                .calendarEvents(testEvents)
                .onDateSelectedListener(this)
                .create();

        calendarListView.setAdapter(adapter);

        adapter.setCalendarEvents(testEvents);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDateSelected(Date date) {
        adapter.addCalendarEvent(new TestEvent(date, null));
        adapter.notifyDataSetChanged();
    }
}




Java Source Code List

io.avocado.android.calendaradapter.app.MainActivity.java
io.avocado.android.calendaradapter.app.TestEvent.java
io.avocado.android.calendaradapter.library.CalendarAdapter.java
io.avocado.android.calendaradapter.library.CalendarCell.java
io.avocado.android.calendaradapter.library.CalendarEvent.java
io.avocado.android.calendaradapter.library.CalendarGrid.java
io.avocado.android.calendaradapter.library.CalendarUtils.java