Calendar View
Calendar View class 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.
Step 1):calendar_main.xml
Create a XML file for CalendarActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="AndroidDhinaBlog"
android:textColor="#000000"
android:textSize="20dp" />
</RelativeLayout>
Create a layout for Listview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:background="#99CCFF"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
Step 3):CalendarActivity
Create a class CalendarActivity.
package com.calenderview;
import android.app.Activity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
public class CalendarActivity extends Activity
{
RelativeLayout relative;
CalendarView calendar;
@SuppressLint({ "NewApi", "NewApi" })
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_main);
relative= (RelativeLayout ) findViewById(R.id.relative);
calendar= new CalendarView (CalendarActivity .this);
RelativeLayout .LayoutParams params =
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
public class CalendarActivity extends Activity
{
RelativeLayout relative;
CalendarView calendar;
@SuppressLint({ "NewApi", "NewApi" })
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_main);
relative= (RelativeLayout ) findViewById(R.id.relative);
calendar= new CalendarView (CalendarActivity .this);
RelativeLayout .LayoutParams params =
new RelativeLayout .LayoutParams((int)
LayoutParams.MATCH_PARENT,(int)LayoutParams.MATCH_PARENT);
params .topMargin = 100;
calendar.setLayoutParams(calendar);
relative.addView(calendar);
calendar.setOnDateChangeListener(new OnDateChangeListener()
{
@Override
public void onSelectedDayChange(CalendarView view,
int year,int month,int day)
{
Toast.makeText(getBaseContext(),"Selected Date is\n\n"+LayoutParams.MATCH_PARENT,(int)LayoutParams.MATCH_PARENT);
params .topMargin = 100;
calendar.setLayoutParams(calendar);
relative.addView(calendar);
calendar.setOnDateChangeListener(new OnDateChangeListener()
{
@Override
public void onSelectedDayChange(CalendarView view,
int year,int month,int day)
{
dayOfMonth+":"+month+":"+year,
Toast.LENGTH_LONG).show();
}
});
});


No comments:
Post a Comment