Calendar View in Android Example


        Android provides calendar view widget that is ready to use one which fulfills our needs when we wish to display a calendar. Since Android by default provides a view and other many attributes to handle calendarview display, life becomes much simple for android developers.

Apart from the default view, we can built our own customizations on top of it. So coming to this tutorial, its nothing but few lines of code will get this done.. Now, lets see how to do this. 

Step 1: Create CalendarView in activitymain.xml
           Place a calendarView in activitymain.xml as shown below.

    <CalendarView
        android:id="@+id/calendView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="60dp" 
     />
 There are many other XML attributes that can be defined, as shown at the bottom of the post.

Step 2: Get Selected Date
            When date is selected the are default functions, that will get invoked. onSelectedDayChange() method is invoked for selecting a date from which we will display a Toast. This Toast will display the selected date as shown in the below screen shot.

public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth) {
// TODO Auto-generated method stub

Toast.makeText(
getBaseContext(),
"Selected Date is\n\n" + dayOfMonth + " / " + month
+ " / " + year, Toast.LENGTH_LONG).show();
}


Very simple example right.. yes, that's it we are done with simple calendar view in android..
Now lets see some additional attributes that can be used to customize the CalendarView.

android:dateTextAppearance The text appearance for the calendar dates. 
android:firstDayOfWeek The first day of week according to Calendar. 
android:focusedMonthDateColor The color for the dates of the focused month. 
android:maxDate The minimal date shown by this calendar view in
 mm/dd/yyyy format. 
android:minDate The minimal date shown by this calendar view in
mm/dd/yyyy format. 
android:selectedDateVerticalBar Drawable for the vertical bar shown at the beginning
 and at the end of the selected date. 
android:selectedWeekBackgroundColor The background color for the selected week. 
android:showWeekNumber Whether do show week numbers. 
android:shownWeekCount The number of weeks to be shown. 
android:unfocusedMonthDateColor The color for the dates of an unfocused month. 
android:weekDayTextAppearance The text appearance for the week day abbreviation
 of the calendar header. 
android:weekNumberColor The color for the week numbers. 
android:weekSeparatorLineColor The color for the separator line between weeks. 


Activity_main.xml

<RelativeLayout 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" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Simple CalendarView"
        android:textColor="#FF6600"
        android:textSize="30dp" />

    <CalendarView
        android:id="@+id/calendView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="60dp" />

</RelativeLayout>

MainActivity.java
  

package com.example.calendarviewandroid;

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

public class MainActivity extends Activity {
CalendarView calend;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calend = (CalendarView) findViewById(R.id.calendView);

calend.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth) {
// TODO Auto-generated method stub
Toast.makeText(
getBaseContext(),
"Selected Date is\n\n" + dayOfMonth + " / " + month
+ " / " + year, Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}




Reactions:

0 comments :

Post a Comment