date Picker Dialog in android

package com.datepic;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatepicActivity extends Activity {
//initializing an integer with some value
int Datepic=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// This method is used to show the date picker which has an integer type parameter

showDialog(Datepic);

}
});

}
protected Dialog onCreateDialog(int id)
{

//creating the calender instance to get the current date,dateofmonth and year. This is used to set current time for the date picker

 
Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);

return new DatePickerDialog(DatepicActivity.this, mdate, year, month, day);

}

 

//setting an

DatePickerDialog.OnDateSetListener mdate=new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub

TextView tv1=(TextView) findViewById(R.id.tv);
String s1,s2,s3;
s1=Integer.toString(dayOfMonth);
s2=Integer.toString(monthOfYear+1);
s3=Integer.toString(year);
tv1.setText(s1+”-“+s2+”-“+s3);

}
};


}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s