May 21, 2013

CalendarView

android.widget.CalendarView, added in API level 11, is 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. To handle user action of changing the date, implement CalendarView.OnDateChangeListener.

android.widget.CalendarView
android.widget.CalendarView


<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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


package com.example.androidcalendarview;

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

public class MainActivity extends Activity {
 
 CalendarView calendar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  calendar = (CalendarView)findViewById(R.id.calendar);
  calendar.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:

Post a Comment

Infolinks In Text Ads