Last active
September 27, 2020 12:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to make OnClickListener on adding event button and add event on click when EditText changes?