Thursday, 6 March 2014

How to Create CalendarView Functionality in Android

In this blog you will learn how to implement CalendarView functionality in your android app.

main.xml

<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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Calendar View"
        android:textColor="#336633"
        android:textSize="20sp"
        android:textStyle="bold" />





<--- Drag CalendarView from Paletee in xml editor from Time & Date -->
  
<CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp" />

</LinearLayout>

CalendarViewActivity.java

package com.rakesh.tiwari.calendarview;

import java.sql.Date;

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

public class CalendarViewActivity extends Activity {
    CalendarView calendar;

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

            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int dayOfMonth) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        "Selected date is :\n\n" + dayOfMonth + "-" + month
                                + "-" + year, Toast.LENGTH_LONG).show();
            }
        });
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.calendarview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rakesh.tiwari.calendarview.CalendarViewActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


CalendarView Functionality

CalendarView Functionality

CalendarView 

CalendarView Implementation

CalendarView Functionality

No comments:

Post a Comment