Android CalendarView

In this Android tip, I am going to show you how to use the CalendarView widget to display a calendar in your Android application. With the CalendarView, you can easily to select a date and scroll from one month to another month. One important event created by CalendarView widget is date selection change event. To receive this event, you will have a class that implements the OnDateChangeListener interface. You will override its onSelectedDayChange method to perform an action when a date is selected from the calendar.

Now to have a workable example application on using CalendarView, you create a new Android project. Then modify the activity_main.xml file to add a CalendarView widget as shown below. The CalendarView widget is available from API level 11. If you get "Call requires API level 11" error, open the AndroidManifest.xml file and look for this line of code: android:minSdkVersion="8". Then change from 8 to 11.

<LinearLayout xmlns:android=""



In the MainActivity class, the code is written to create a reference to the CalendarView, receiving the date selection change event. In this example, the selected date is displayed using Toast message.

package com.example.apps;

import android.os.Bundle;
import android.view.Menu;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;

 public class MainActivity extends Activity {

Activity context;
protected void onCreate(Bundle savedInstanceState) {
//reference to calendar view
CalendarView calendar_view=(CalendarView)findViewById(;
//receiving date selection change event
calendar_view.setOnDateChangeListener(new DateChange());

private class DateChange implements OnDateChangeListener{
public void onSelectedDayChange(CalendarView view, int year,int month,int day){
Toast.makeText(context, year+"/"+month+"/"+day , Toast.LENGTH_SHORT).show();

public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;


Posted by: Dara | post date: 08-12-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.