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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >

   <CalendarView
     android:id="@+id/calendar_view"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"    
  
      />
  

</LinearLayout>


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.app.Activity;
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
//reference to calendar view
CalendarView calendar_view=(CalendarView)findViewById(R.id.calendar_view);
//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();
}
}

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


Android-CalendarVew

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.