This tutorial for you to share the use of the Android Calendar library for your reference, the details are as follows
Mainactivity.java Code:
Package siso.weekv;
Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
public class Mainactivity extends Appcompatactivity {
@Override
protected void OnCreate (Bundle Savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Findviewbyid (R.id.buttonbasic). Setonclicklistener (New View.onclicklistener () {
@Override public
Void OnClick (View v) {
Intent Intent = new Intent (mainactivity.this, basicactivity.class);
StartActivity (intent);
}
);
Findviewbyid (r.id.buttonasynchronous). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent (mainactivity.this, asynchronousactivity.class);
StartActivity (intent);}}
);
}
Asynchronousactivity.java Code:
Package siso.weekv;
Import Android.widget.Toast;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import java.util.List; Import Retrofit.
Callback; Import Retrofit.
Restadapter; Import Retrofit.
Retrofiterror;
Import Retrofit.client.Response;
Import siso.weekv.apiclient.Event;
Import Siso.weekv.apiclient.MyJsonService;
Import siso.wvlib.WeekViewEvent;
/** * A example of how events can is fetched from network and is displayed on the Week view.
* Created by Raquib-ul-alam Kanak on 1/3/2014. * Website:http://alamkanak.github.io/public class Asynchronousactivity extends baseactivity implements Callback<l
ist<event>> {private List<weekviewevent> events = new arraylist<weekviewevent> ();
Boolean callednetwork = false; @Override public list<? Extends weekviewevent> onmonthchange (int newyear, int newmonth) {//Download events from Network if it hasn ' t been Done already. To understand how events are//downloaded using retrofit, visitHttp://square.github.io/retrofit if (!callednetwork) {Restadapter retrofit = new Restadapter.builder (). SETENDP
Oint ("Https://api.myjson.com/bins"). Build ();
Myjsonservice service = retrofit.create (Myjsonservice.class);
Service.listevents (this);
Callednetwork = true;
//return only to the events that matches NewYear and Newmonth.
list<weekviewevent> matchedevents = new arraylist<weekviewevent> ();
for (Weekviewevent event:events) {if (Eventmatches (event, NewYear, Newmonth)) {Matchedevents.add (event);
} return matchedevents;
/** * Checks If an event falls into a specific year and month.
* @param event the event to check for.
* @param year of the year.
* @param month the month.
* @return True If the event matches the year and month. * * Private Boolean eventmatches (Weekviewevent event, int year, int month) {return (Event.getstarttime (). Get (Calendar.y EAR) = = year && event.getstarttime (). Get (Calendar.month) = = Month-1) | |
(Event.getendtime (). Get (calendar.year) = = year && event.getendtime (). Get (calendar.month) = = month-1);
@Override public void Success (list<event> events, Response Response) {this.events.clear ();
for (Event event:events) {This.events.add (event.toweekviewevent ());
} getweekview (). notifydatasetchanged ();
@Override public void failure (Retrofiterror error) {error.printstacktrace ();
Toast.maketext (This, R.string.async_error, Toast.length_short). Show ();
}
}
Baseactivity.java
Package siso.weekv;
Import Android.graphics.RectF;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.TypedValue;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.Toast;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Locale;
Import Siso.wvlib.DateTimeInterpreter;
Import Siso.wvlib.MonthLoader;
Import Siso.wvlib.WeekView;
Import siso.wvlib.WeekViewEvent; Public abstract class Baseactivity extends Appcompatactivity implements Weekview.eventclicklistener, Monthloader.monthchangelistener, Weekview.eventlongpresslistener, Weekview.emptyviewlongpresslistener {private
static final int type_day_view = 1;
private static final int type_three_day_view = 2;
private static final int type_week_view = 3;
private int mweekviewtype = Type_three_day_view;
Private Weekview Mweekview;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); SEtcontentview (r.layout.activity_base);
Get a reference for the week view in the layout.
Mweekview = (Weekview) Findviewbyid (R.id.weekview);
Show a toast message about the touched event.
Mweekview.setoneventclicklistener (this);
Mweekview.setmonthchangelistener (this);
Set Long press listener for events.
Mweekview.seteventlongpresslistener (this);
Mweekview.setemptyviewlongpresslistener (this);
Setupdatetimeinterpreter (FALSE);
@Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu);
return true;
@Override public boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid ();
Setupdatetimeinterpreter (id = = R.id.action_week_view);
Switch (ID) {case r.id.action_today:mweekview.gototoday ();
return true;
Case R.id.action_day_view:if (Mweekviewtype!= type_day_view) {item.setchecked (!item.ischecked ());
Mweekviewtype = Type_day_view; Mweekview.setnUmberofvisibledays (1);
Lets change some dimensions to best fit the view. Mweekview.setcolumngap ((int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 8, getresources ().
Getdisplaymetrics ())); Mweekview.settextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, Getresources ().
Getdisplaymetrics ())); Mweekview.seteventtextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, GetResources ().
Getdisplaymetrics ()));
return true;
Case R.id.action_three_day_view:if (Mweekviewtype!= type_three_day_view) {item.setchecked (!item.isChecked ());
Mweekviewtype = Type_three_day_view;
Mweekview.setnumberofvisibledays (3);
Lets change some dimensions to best fit the view. Mweekview.setcolumngap ((int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 8, getresources ().
Getdisplaymetrics ())); Mweekview.settextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, Getresources (). Getdisplaymetrics ()); Mweekview.seteventtextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, GetResources ().
Getdisplaymetrics ()));
return true;
Case R.id.action_week_view:if (Mweekviewtype!= type_week_view) {item.setchecked (!item.ischecked ());
Mweekviewtype = Type_week_view;
Mweekview.setnumberofvisibledays (7); Mweekview.setcolumngap ((int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 2, getresources ().
Getdisplaymetrics ())); Mweekview.settextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, Getresources ().
Getdisplaymetrics ())); Mweekview.seteventtextsize ((int) typedvalue.applydimension (typedvalue.complex_unit_sp, GetResources ().
Getdisplaymetrics ()));
return true;
return super.onoptionsitemselected (item); } private void Setupdatetimeinterpreter (final Boolean shortdate) {mweekview.setdatetimeinterpreter (new Datetimeinter Preter () {@Override publicString interpretdate (Calendar date) {SimpleDateFormat Weekdaynameformat = new SimpleDateFormat ("EEE", Locale.getdefau
LT ());
String weekday = Weekdaynameformat.format (Date.gettime ());
SimpleDateFormat format = new SimpleDateFormat ("M/d", Locale.getdefault ());
if (shortdate) weekday = string.valueof (Weekday.charat (0));
return weekday.touppercase () + Format.format (Date.gettime ()); @Override public String interprettime (int hour) {return hour > 11? (hour-12) + "PM": (hour = 0?)
"AM": Hour + "AM");
}
}); } protected String Geteventtitle (Calendar time) {return String.Format ("Event%02d:%02d%s/%d", Time.get (calendar.hour_of
_day), Time.get (Calendar.minute), Time.get (Calendar.month) +1, Time.get (Calendar.day_of_month)); @Override public void Oneventclick (Weekviewevent event, RECTF eventrect) {Toast.maketext (This, "click" + Event.getna
Me (), Toast.length_short). Show (); @Override public void Oneventlongpress (WeekviewevEnt event, RECTF eventrect) {Toast.maketext (this, "Long by Event:" + event.getname (), Toast.length_short). Show (); @Override public void Onemptyviewlongpress (the Calendar time) {Toast.maketext (this, empty view long press: + Geteventtitle (time), T Oast.
Length_short). Show ();
Public Weekview Getweekview () {return mweekview;
}
}
Basicactivity.java
Package siso.weekv;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import java.util.List;
Import siso.wvlib.WeekViewEvent; public class Basicactivity extends Baseactivity {@Override public list<? extends Weekviewevent> Onmonthchange (in
T newyear, int newmonth) {//Populate the Week view with some events.
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, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_01));
Events.add (event);
StartTime = Calendar.getinstance (); Starttime.set (calendar.hour_of_dAY, 3);
Starttime.set (Calendar.minute, 30);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.set (Calendar.hour_of_day, 4);
Endtime.set (Calendar.minute, 30);
Endtime.set (Calendar.month, newMonth-1);
event = new Weekviewevent (Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_02));
Events.add (event);
StartTime = Calendar.getinstance ();
Starttime.set (Calendar.hour_of_day, 4);
Starttime.set (Calendar.minute, 20);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.set (Calendar.hour_of_day, 5);
Endtime.set (calendar.minute, 0);
event = new Weekviewevent (Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_03));
Events.add (event);
StartTime = Calendar.getinstance (); StartTime.Set (Calendar.hour_of_day, 5);
Starttime.set (Calendar.minute, 30);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.add (Calendar.hour_of_day, 2);
Endtime.set (Calendar.month, newMonth-1);
event = new Weekviewevent (2, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_02));
Events.add (event);
StartTime = Calendar.getinstance ();
Starttime.set (Calendar.hour_of_day, 5);
Starttime.set (calendar.minute, 0);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Starttime.add (calendar.date, 1);
Endtime = (Calendar) starttime.clone ();
Endtime.add (Calendar.hour_of_day, 3);
Endtime.set (Calendar.month, newMonth-1);
event = new Weekviewevent (3, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_03));
Events.add (event); StartTime = Calendar. getinstance ();
Starttime.set (Calendar.day_of_month, 15);
Starttime.set (Calendar.hour_of_day, 3);
Starttime.set (calendar.minute, 0);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.add (Calendar.hour_of_day, 3);
event = new Weekviewevent (4, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_04));
Events.add (event);
StartTime = Calendar.getinstance ();
Starttime.set (Calendar.day_of_month, 1);
Starttime.set (Calendar.hour_of_day, 3);
Starttime.set (calendar.minute, 0);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.add (Calendar.hour_of_day, 3);
event = new Weekviewevent (5, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_01));
Events.add (event); StartTime = Calendar.getinstaNCE ();
Starttime.set (Calendar.day_of_month, Starttime.getactualmaximum (Calendar.day_of_month));
Starttime.set (Calendar.hour_of_day, 15);
Starttime.set (calendar.minute, 0);
Starttime.set (Calendar.month, newMonth-1);
Starttime.set (Calendar.year, newyear);
Endtime = (Calendar) starttime.clone ();
Endtime.add (Calendar.hour_of_day, 3);
event = new Weekviewevent (5, Geteventtitle (starttime), StartTime, Endtime);
Event.setcolor (Getresources (). GetColor (r.color.event_color_02));
Events.add (event);
return events;
}
}
Apiclient folder under Event.java and Myjsonservice.java
Package siso.weekv.apiclient;
Import Android.annotation.SuppressLint;
Import Android.graphics.Color;
Import Com.google.gson.annotations.Expose;
Import Com.google.gson.annotations.SerializedName;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import siso.wvlib.WeekViewEvent;
public class Event {@Expose @SerializedName ("name") private String mname;
@Expose @SerializedName ("DayOfMonth") private int mdayofmonth;
@Expose @SerializedName ("StartTime") private String mstarttime;
@Expose @SerializedName ("Endtime") private String mendtime;
@Expose @SerializedName ("color") private String Mcolor;
Public String GetName () {return mname;
public void SetName (String name) {this.mname = name;
public int GetDayOfMonth () {return mdayofmonth;
The public void setdayofmonth (int dayofmonth) {this.mdayofmonth = DayOfMonth;
Public String GetStartTime () {return mstarttime; } public voidSetstarttime (String starttime) {this.mstarttime = StartTime;
Public String Getendtime () {return mendtime;
} public void Setendtime (String endtime) {this.mendtime = Endtime;
Public String GetColor () {return mcolor;
public void SetColor (String color) {this.mcolor = color;
@SuppressLint ("SimpleDateFormat") public weekviewevent toweekviewevent () {//Parse time.
SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
Date start = new Date ();
Date end = new Date ();
try {start = Sdf.parse (GetStartTime ());
catch (ParseException e) {e.printstacktrace ();
try {end = Sdf.parse (Getendtime ());
catch (ParseException e) {e.printstacktrace ();
}//Initialize start and end time.
Calendar now = Calendar.getinstance ();
Calendar starttime = (Calendar) now.clone ();
Starttime.settimeinmillis (Start.gettime ());
Starttime.set (Calendar.year, Now.get (calendar.year)); Starttime.set (Calendar.month, Now.get (Calendar.month));
Starttime.set (Calendar.day_of_month, GetDayOfMonth ());
Calendar Endtime = (Calendar) starttime.clone ();
Endtime.settimeinmillis (End.gettime ());
Endtime.set (Calendar.year, Starttime.get (calendar.year));
Endtime.set (Calendar.month, Starttime.get (Calendar.month));
Endtime.set (Calendar.day_of_month, Starttime.get (Calendar.day_of_month));
Create an week view event.
Weekviewevent weekviewevent = new Weekviewevent ();
Weekviewevent.setname (GetName ());
Weekviewevent.setstarttime (starttime);
Weekviewevent.setendtime (Endtime);
Weekviewevent.setcolor (Color.parsecolor (GetColor ()));
return weekviewevent;
}
}
Package siso.weekv.apiclient;
Import java.util.List;
Import Retrofit. Callback;
Import Retrofit.http.GET;
/**
* Created by Raquib-ul-alam Kanak on 1/3/16.
* Website:http://alamkanak.github.io/Public
interface Myjsonservice {
//https://api.myjson.com/bins /3nh0k
@GET ("/3nh0k")
void Listevents (callback<list<event>> eventscallback);
}
APP Project structure:
Andriod Class Library Project structure:
The results of the project run as shown:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.