Skip to content
Till Hellmund edited this page Mar 17, 2019 · 26 revisions

Getting started

Add the JitPack repository to your project-level build.gradle file and the dependency to the app-level build.gradle file.

// build.gradle (project-level)
allprojects {
 repositories {
  ...
  maven { url 'https://jitpack.io' }
 }
}

// build.gradle (app-level)
implementation 'com.github.thellmund:Android-Week-View:3.0'

Usage

First, become familiar with the WeekView nomenclature.

You need to add a WeekView in your XML layout file. You can find a list of all available attributes under Attributes.

<com.alamkanak.weekview.ui.WeekView
    android:id="@+id/weekView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:eventTextColor="@color/white"
    app:textSize="12sp"
    app:hourHeight="60dp"
    app:headerColumnPadding="8dp"
    app:headerColumnTextColor="@color/light_blue"
    app:headerRowPadding="12dp"
    app:columnGap="8dp"
    app:numberOfVisibleDays="3"
    app:headerRowBackgroundColor="@color/light_gray"
    app:dayBackgroundColor="@color/white"
    app:todayBackgroundColor="@color/light_blue"
    app:headerColumnBackground="@color/white" />

Next, you need to prepare the class of objects that you want to display in WeekView. Consider that you’re fetching a list of CalendarItems from a database. This class needs to implement WeekViewDisplayable, which describes elements that can be displayed in WeekView. For WeekView to work correctly, you need to pass the original class to WeekViewDisplayable as the generic class. The final result will look like this:

public class CalendarItem implements WeekViewDisplayable<CalendarItem> {

    private long id;
    private String title;
    private DateTime startTime;
    private DateTime endTime;
    private String location;
    private int color;
    
    /* ... */
    
    @Override
    public WeekViewEvent<CalendarItem> toWeekViewEvent() {
        // Note: It's important to pass "this" as the last argument to 
        // WeekViewEvent's constructor. This way, the EventClickListener 
        // can return this object in its onEventClick() method.
        boolean isAllDay = DateUtils.isAllDay(startTime, endTime);
        return new WeekViewEvent<>(
            id, title, startTime.toGregorianCalendar(), 
            endTime.toGregorianCalendar(), location, color, isAllDay, this
        );
    }

}

Lastly, you can attach various listeners to a WeekView. The example code below highlights the most common ones. For a full list of available listeners, check out Listeners.

WeekView<CalendarItem> weekView = (WeekView) findViewById(R.id.weekView);
weekView.setOnEventClickListener(new EventClickListener<CalendarItem>() {
    @Override
    public void onEventClick(CalendarItem event, RectF eventRect) {
        // Do something with the CalendarItem
    }
});

// WeekView has infinite horizontal scrolling. Therefore, you need to provide the events 
// of a month whenever that the currently displayed month changes.
weekView.setMonthChangeListener(new MonthChangeListener<CalendarItem>() {
    @Override
    public List<WeekViewDisplayable<CalendarItem>> onMonthChange(Calendar startDate, Calendar endDate) {
        return mDatabase.getCalendarItemsInRange(startDate, endDate);
    }
});
Clone this wiki locally