Friday, January 10, 2014
How To create DatePickerDialog Example in Android
Manchu Bhargavi
9:09 PM
datedialog
,
datelistener
,
datepickerdialog
,
how to create an dilaog
,
ondatset
16 comments
:
Hi,
This is an Example for creating DatePickerDialog Example in Android
DatePicker Dialog is child class for AlertDialog.It is used to select Date.And the benefits of DatePickerDialog is appears as pre user request and after setting the date it automatically disappears.And it does not occupy any space in Activity.
Syntax for Create DatePickerDialog :
Create a DatePickerDailog class and the parameter of DatePickerDialog is Activity Context,Callback Listener,year,month,day.
Dialog dialog=new DatePickerDialog(context,callback,year,month,dayofMonth);
activity_main.xml:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="142dp"
android:text="StartDate"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="EndDate"
android:textStyle="bold"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_toLeftOf="@+id/startdate"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:hint="Startdate" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignTop="@+id/textView2"
android:layout_toLeftOf="@+id/startdate"
android:hint="Enddate"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/startdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:background="@drawable/img" />
<Button
android:id="@+id/enddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_toRightOf="@+id/editText2"
android:background="@drawable/img"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="submit" />
</RelativeLayout>
In the activity class I have created a constant DATE_DIALOG_ID
static final int DATE_DIALOG_ID=0;
It is unique id for the DatePickerDialog. It can be any number. Now we can discuss about showDatePickerDialog method
mstart_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
showDialog(START_DATE_DIALOG_ID);
}
});
First Iam getting current date status and storing to instance variables year,month,day. To display a dialog window we are using showDialog method and Iam also passing the unique id.
This showDialog method is comes from android.app.Activity class.
showDialog(START_DATE_DIALOG_ID);
when the showDialog method is called,the onCreateDialog method will be automatically called.
protected Dialog onCreateDialog(int id){
switch(id){
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,dataSetListener,year, month, day);
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,endSetListener,year, month, day);
}
return null;
}
In the onCreateDialog method. creating the DatePickerDialog. Then return it to the calling location and dialog will be displayed automatically.
In the DatePickerDialog constructor passing the value for year,month,day and passing the reference of DatePickerDialog.OnDateSetListener.
In OnDateSetListener is automatically called onDateSet method when user click the on Set button of date picker dialog.
onDateSet method assign the value for year,month,day and set the Text it.
private DatePickerDialog.OnDateSetListener dataSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year=year;
month=monthOfYear;
day=dayOfMonth;
mstart_edt1.setText(day+" - "+(month+1)+" - "+year);
}
};
Iam calling setText method of EditText by passing the selected year,month and day
Here the output is shown below
.
This is an Example for creating DatePickerDialog Example in Android
DatePicker Dialog is child class for AlertDialog.It is used to select Date.And the benefits of DatePickerDialog is appears as pre user request and after setting the date it automatically disappears.And it does not occupy any space in Activity.
Syntax for Create DatePickerDialog :
Create a DatePickerDailog class and the parameter of DatePickerDialog is Activity Context,Callback Listener,year,month,day.
Dialog dialog=new DatePickerDialog(context,callback,year,month,dayofMonth);
activity_main.xml:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="142dp"
android:text="StartDate"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="EndDate"
android:textStyle="bold"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_toLeftOf="@+id/startdate"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:hint="Startdate" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignTop="@+id/textView2"
android:layout_toLeftOf="@+id/startdate"
android:hint="Enddate"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/startdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:background="@drawable/img" />
<Button
android:id="@+id/enddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_toRightOf="@+id/editText2"
android:background="@drawable/img"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="submit" />
</RelativeLayout>
MainActivity.java:
package com.example.datepickerexample;
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.EditText;
public class MainActivity extends Activity {
Button mstart_date,mend_date;
private int year;
private int month;
private int day;
Calendar cal;
EditText mstart_edt1,mend_edt2;
static final int START_DATE_DIALOG_ID=0;
static final int END_DATE_DIALOG_ID=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mstart_edt1=(EditText)findViewById(R.id.editText1);
mend_edt2=(EditText)findViewById(R.id.editText2);
mstart_date=(Button)findViewById(R.id.startdate);
mend_date=(Button)findViewById(R.id.enddate);
mstart_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
showDialog(START_DATE_DIALOG_ID);
}
});
mend_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cal = Calendar.getInstance();
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH);
day=cal.get(Calendar.DAY_OF_MONTH);
showDialog(END_DATE_DIALOG_ID);
}
});
}
protected Dialog onCreateDialog(int id){
switch(id){
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,dataSetListener,year, month, day);
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,endSetListener,year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener dataSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year=year;
month=monthOfYear;
day=dayOfMonth;
mstart_edt1.setText(day+" - "+(month+1)+" - "+year);
}
};
private DatePickerDialog.OnDateSetListener endSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year=year;
month=monthOfYear;
day=dayOfMonth;
mend_edt2.setText(day+" - "+(month+1)+" - "+year);
}
};
}
Here the explanation for Mainactivity.class
In the activity class I have created a constant DATE_DIALOG_ID
static final int DATE_DIALOG_ID=0;
It is unique id for the DatePickerDialog. It can be any number. Now we can discuss about showDatePickerDialog method
mstart_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
showDialog(START_DATE_DIALOG_ID);
}
});
First Iam getting current date status and storing to instance variables year,month,day. To display a dialog window we are using showDialog method and Iam also passing the unique id.
This showDialog method is comes from android.app.Activity class.
showDialog(START_DATE_DIALOG_ID);
when the showDialog method is called,the onCreateDialog method will be automatically called.
protected Dialog onCreateDialog(int id){
switch(id){
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,dataSetListener,year, month, day);
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,endSetListener,year, month, day);
}
return null;
}
In the onCreateDialog method. creating the DatePickerDialog. Then return it to the calling location and dialog will be displayed automatically.
In the DatePickerDialog constructor passing the value for year,month,day and passing the reference of DatePickerDialog.OnDateSetListener.
In OnDateSetListener is automatically called onDateSet method when user click the on Set button of date picker dialog.
onDateSet method assign the value for year,month,day and set the Text it.
private DatePickerDialog.OnDateSetListener dataSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year=year;
month=monthOfYear;
day=dayOfMonth;
mstart_edt1.setText(day+" - "+(month+1)+" - "+year);
}
};
Iam calling setText method of EditText by passing the selected year,month and day
Here the output is shown below
.
Subscribe to:
Post Comments
(
Atom
)
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeleteData Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeletejava training in chennai | java training in bangalore
java online training | java training in pune
java training in chennai | java training in bangalore
Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
ReplyDeletepython training in pune
python online training
python training in OMR
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
ReplyDeleteBlueprism training institute in Chennai
Blueprism online training
This is my 1st visit to your web... But I'm so impressed with your content. Good Job!
ReplyDeleteangularjs Training in chennai
angularjs-Training in pune
angularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
angularjs-Training in sholinganallur
This comment has been removed by the author.
ReplyDeleteHmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
ReplyDeleteAWS Interview Questions And Answers
AWS Training in Bangalore | Amazon Web Services Training in Bangalore
AWS Training in Pune | Best Amazon Web Services Training in Pune
Amazon Web Services Training in Pune | Best AWS Training in Pune
AWS Online Training | Online AWS Certification Course - Gangboard
Great stuff!! Keep doing and I would like to share with my friends
ReplyDeleteSelenium Training in Chennai
Selenium Training
iOS Training in Chennai
Digital Marketing Training in Chennai
core java training in chennai
Salesforce Training in Chennai
Salesforce Training
Big Data Training in Chennai
I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
"Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
Thank you so much for sharing these amazing tips. I must say you are an incredible writer, I love the way that you describe the things. Please keep sharing.
ReplyDeletejava training in chennai
java training in velachery
aws training in chennai
aws training in velachery
python training in chennai
python training in velachery
selenium training in chennai
selenium training in velachery
Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteoracle training in chennai
oracle training in annanagar
oracle dba training in chennai
oracle dba training in annanagar
ccna training in chennai
ccna training in annanagar
seo training in chennai
seo training in annanagar
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command..
ReplyDeletedata science training in chennai
data science training in omr
android training in chennai
android training in omr
devops training in chennai
devops training in omr
artificial intelligence training in chennai
artificial intelligence training in omr
Title:
ReplyDeleteGrab Oracle Certification in Chennai | Infycle Technologies
Description:
Want to get Oracle Certification with the job opportunities? Infycle is with you for this! Infycle Technologies gives the most trustworthy training for the Oracle Certification in Chennai, which will be guided by professional tutors in the field. Along with that, the mock interviews will be assigned for the candidates, so that, they can meet the job interviews with full confidence. To transform your career to the next level, call 7502633633 to Infycle Technologies and grab a free demo to get more.
Best training in Chennai
Title:
ReplyDeleteBest Oracle Training Institute in Chennai | Infycle Technologies
Description:
Set your career goal towards Oracle for a wealthy future with Infycle. Infycle Technologies is the best Oracle training institute in Chennai, which gives the most trusted and best Oracle Training in hands-on practical training that will be guided by professional tutors in the field. In addition to this, the mock interviews will be given to the candidates, so that, they can face the interviews with full confidence. Apart from all, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.
Best training in Chennai
Set your career goal towards Oracle for a wealthy future with Infycle. Infycle Technologies is the best Oracle training institute in Chennai, which gives the most trusted and best Oracle Training in hands-on practical training that will be guided by professional tutors in the field. In addition to this, the mock interviews will be given to the candidates, so that, they can face the interviews with full confidence. Apart from all, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.
ReplyDeletebest training institute in chennai