Highlight multiple dates with Android Calendar View

Asked

Viewed 1,502 times

1

My app logs events with date and time on sqlite, has a menu that will show all these records in a calendar, marking the dates (no schedules) in the calendar, as an event app... When the user clicks on the marked date a new one will open Activity showing the details of that record, (date, time, name, etc).

I can set a single date with the setDate(long) of CalendarView, but how can I set several dates? Or have some way to mark the dates that have some record with some prominent color?

1 answer

2


I searched and found a third-party api for that... Her calendar is much more attractive and easy to use, searching a lot saw that can not highlight several dates with Android Calendarview...

Create a class that extends and implements the Weekview class and interfaces:

public class ClipperCalendar extends WeekView implements WeekView.EventClickListener, MonthLoader.MonthChangeListener, WeekView.EventLongPressListener, WeekView.EmptyViewLongPressListener {

    private Context context;

    public ClipperCalendar(Context context) {
        super(context);
        this.context = context;
        setOnEventClickListener(this);
        setMonthChangeListener(this);
    }

    @Override
    public void onEmptyViewLongPress(Calendar time) {
        Toast.makeText(context, "View vazia press: "+time.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEventClick(WeekViewEvent event, RectF eventRect) {
        Toast.makeText(context, "Clicked " + event.getName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEventLongPress(WeekViewEvent event, RectF eventRect) {
        Toast.makeText(context, "LongPress " + event.getName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
        // Popular a lista com eventos
        List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();

        Calendar startTime = Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY, 3);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.MONTH, newMonth - 1);
        startTime.set(Calendar.YEAR, newYear);
        Calendar endTime = (Calendar) startTime.clone();
        endTime.add(Calendar.HOUR, 1);
        endTime.set(Calendar.MONTH, newMonth - 1);
        WeekViewEvent event = new WeekViewEvent(1, "Titulo", startTime, endTime);
        event.setColor(getResources().getColor(android.R.color.holo_blue_bright));
        events.add(event);

        return events;
    }
}

Initialize it in your Activity, remembering that you have to set the widget in the layout:

<com.alamkanak.weekview.WeekView
        android:id="@+id/weekView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:eventTextColor="@android:color/white"
        app:textSize="12sp"
        app:hourHeight="60dp"
        app:headerColumnPadding="8dp"
        app:headerColumnTextColor="#8f000000"
        app:headerRowPadding="12dp"
        app:columnGap="8dp"
        app:noOfVisibleDays="3"
        app:headerRowBackgroundColor="#ffefefef"
        app:dayBackgroundColor="#05000000"
        app:todayBackgroundColor="#1848adff"
        app:headerColumnBackground="#ffffffff"/>

Launch the Widget and the Calendar class created by you:

mWeekView = (WeekView) view.findViewById(R.id.weekView);
clipperCalendar = new ClipperCalendar(getContext());

API link: https://github.com/alamkanak/Android-Week-View

Browser other questions tagged

You are not signed in. Login or sign up in order to post.