Skip to content

Instantly share code, notes, and snippets.

@alamkanak
Last active September 27, 2020 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alamkanak/eb67870f0b7a6001e188 to your computer and use it in GitHub Desktop.
Save alamkanak/eb67870f0b7a6001e188 to your computer and use it in GitHub Desktop.
Adding events on week view calendar by tapping in empty view. Get the week view calendar from https://github.com/alamkanak/Android-Week-View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#fff">
<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"/>
</RelativeLayout>
package com.alamkanak.weekview.sample;
import android.app.Activity;
import android.os.Bundle;
import com.alamkanak.weekview.WeekView;
import com.alamkanak.weekview.WeekViewEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
* Website: http://alamkanak.github.io
*/
public class MainActivity extends Activity implements WeekView.MonthChangeListener, WeekView.EmptyViewClickListener {
private WeekView mWeekView;
private ArrayList<WeekViewEvent> mNewEvents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a reference for the week view in the layout.
mWeekView = (WeekView) findViewById(R.id.weekView);
// The week view has infinite scrolling horizontally. We have to provide the events of a
// month every time the month changes on the week view.
mWeekView.setMonthChangeListener(this);
// Set up empty view click listener.
mWeekView.setEmptyViewClickListener(this);
// Initially, there will be no events on the week view because the user has not tapped on
// it yet.
mNewEvents = new ArrayList<WeekViewEvent>();
}
@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
// Populate the week view with the events that was added by tapping on empty view.
List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
ArrayList<WeekViewEvent> newEvents = getNewEvents(newYear, newMonth);
events.addAll(newEvents);
return events;
}
/**
* Get events that were added by tapping on empty view.
* @param year The year currently visible on the week view.
* @param month The month currently visible on the week view.
* @return The events of the given year and month.
*/
private ArrayList<WeekViewEvent> getNewEvents(int year, int month) {
// Get the starting point and ending point of the given month. We need this to find the
// events of the given month.
Calendar startOfMonth = Calendar.getInstance();
startOfMonth.set(Calendar.YEAR, year);
startOfMonth.set(Calendar.MONTH, month - 1);
startOfMonth.set(Calendar.DAY_OF_MONTH, 1);
startOfMonth.set(Calendar.HOUR_OF_DAY, 0);
startOfMonth.set(Calendar.MINUTE, 0);
startOfMonth.set(Calendar.SECOND, 0);
startOfMonth.set(Calendar.MILLISECOND, 0);
Calendar endOfMonth = (Calendar) startOfMonth.clone();
endOfMonth.set(Calendar.DAY_OF_MONTH, endOfMonth.getMaximum(Calendar.DAY_OF_MONTH));
endOfMonth.set(Calendar.HOUR_OF_DAY, 23);
endOfMonth.set(Calendar.MINUTE, 59);
endOfMonth.set(Calendar.SECOND, 59);
// Find the events that were added by tapping on empty view and that occurs in the given
// time frame.
ArrayList<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
for (WeekViewEvent event : mNewEvents) {
if (event.getEndTime().getTimeInMillis() > startOfMonth.getTimeInMillis() &&
event.getStartTime().getTimeInMillis() < endOfMonth.getTimeInMillis()) {
events.add(event);
}
}
return events;
}
@Override
public void onEmptyViewClicked(Calendar time) {
// Set the new event with duration one hour.
Calendar endTime = (Calendar) time.clone();
endTime.add(Calendar.HOUR, 1);
// Create a new event.
WeekViewEvent event = new WeekViewEvent(20, "New event", time, endTime);
mNewEvents.add(event);
// Refresh the week view. onMonthChange will be called again.
mWeekView.notifyDatasetChanged();
}
}
@Jayelef
Copy link

Jayelef commented Sep 30, 2015

It works properly and it's fantastic. Can someone tell me how to put (or convert) the parameters of the event just created? I need to get the date of new event, the starttime and end time in format yyyy-mm-dd and hour in hh:mm but i don't be able to retrieve this data. Your help would be apreciated. Thanks in advance

@jamessmith36
Copy link

I know its almost a year later. But to format Calendar etc. Use something like SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String date1 = format1.format(date);
That should get you started. But you might not want to pass it into a String for what your needing.

@elik21
Copy link

elik21 commented Sep 27, 2020

how to make OnClickListener on adding event button and add event on click when EditText changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment