Sunday, May 17, 2015

CalendarView example

A simple example of CalendarView (added in API level 11), a calendar widget for displaying and selecting dates. The range of dates supported by this calendar is configurable. A user can select a date by taping on it and can scroll and fling the calendar to a desired date.



<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"
    android:orientation="vertical"
    tools:context="com.example.androidcalendarview.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

package com.example.androidcalendarview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
 
 CalendarView myCalendarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myCalendarView = (CalendarView)findViewById(R.id.calendar);
        
        myCalendarView.setOnDateChangeListener(new OnDateChangeListener(){

   @Override
   public void onSelectedDayChange(CalendarView view, int year,
     int month, int dayOfMonth) {
    Toast.makeText(getApplicationContext(), 
           "Year: " + year + "\n" +
           "Month: " + month + "\n" +
           "Day of Month: " + dayOfMonth, 
           Toast.LENGTH_LONG).show();
   }});
    }

}


No comments: