Highlight multiple dates with Android Calendar View

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

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

    
asked by anonymous 15.02.2016 / 01:32

1 answer

2

I searched and found a third-party api for this ... Her calendar is a lot more attractive and easy to use, I've been researching a lot since I did not want to highlight multiple dates with the CalendarView of Android ...

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"/>

Start the Widget and the calendar class you created:

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

API link: link

    
16.02.2016 / 02:18