In this Android Example, we will see how to open a DatePickerDialog on EditText click event, select the date from the Calendar, and set it in EditText in dd-MM-yyyy format using SimpleDateFormat.
Android Project
Create a new Android project and name it as DatePickerDialogEditTextClickEvent .
Resources
Layout Files
activity_main.xml
This XML layout file (activity_main.xml) is used for defining EditText. Open activity_main.xml file in res/layout and copy the following content.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<
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
:
weightSum
=
"1"
tools
:
context
=
".MainActivity"
>
<
EditText
android
:
id
=
"@+id/etxt_fromdate"
android
:
layout_width
=
"0dp"
android
:
layout_height
=
"wrap_content"
android
:
layout_weight
=
".5"
android
:
hint
=
"@string/from_date"
/
>
<
EditText
android
:
id
=
"@+id/etxt_todate"
android
:
layout_width
=
"0dp"
android
:
layout_height
=
"wrap_content"
android
:
layout_weight
=
".5"
android
:
hint
=
"@string/to_date"
/
>
<
/
LinearLayout
>
|
strings.xml
Open res/values/strings.xml and replace it with following content.
|
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
string
name
=
"app_name"
>
DatePickerDialogEditTextClickEvent
<
/
string
>
<
string
name
=
"action_settings"
>
Settings
<
/
string
>
<
string
name
=
"hello_world"
>
Hello
world
!
<
/
string
>
<
string
name
=
"from_date"
>
From
Date
<
/
string
>
<
string
name
=
"to_date"
>
To
Date
<
/
string
>
<
/
resources
>
|
Source files
Activity Class
Open your activity class (MainActivity.java) and copy the following code.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package
com
.
androidopentutorials
.
datepickeredittext
;
import
java
.
text
.
SimpleDateFormat
;
import
java
.
util
.
Calendar
;
import
java
.
util
.
Locale
;
import
android
.
app
.
Activity
;
import
android
.
app
.
DatePickerDialog
;
import
android
.
app
.
DatePickerDialog
.
OnDateSetListener
;
import
android
.
os
.
Bundle
;
import
android
.
text
.
InputType
;
import
android
.
view
.
Menu
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnClickListener
;
import
android
.
widget
.
DatePicker
;
import
android
.
widget
.
EditText
;
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
//UI References
private
EditText
fromDateEtxt
;
private
EditText
toDateEtxt
;
private
DatePickerDialog
fromDatePickerDialog
;
private
DatePickerDialog
toDatePickerDialog
;
private
SimpleDateFormat
dateFormatter
;
@
Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
activity_main
)
;
dateFormatter
=
new
SimpleDateFormat
(
"dd-MM-yyyy"
,
Locale
.
US
)
;
findViewsById
(
)
;
setDateTimeField
(
)
;
}
private
void
findViewsById
(
)
{
fromDateEtxt
=
(
EditText
)
findViewById
(
R
.
id
.
etxt_fromdate
)
;
fromDateEtxt
.
setInputType
(
InputType
.
TYPE_NULL
)
;
fromDateEtxt
.
requestFocus
(
)
;
toDateEtxt
=
(
EditText
)
findViewById
(
R
.
id
.
etxt_todate
)
;
toDateEtxt
.
setInputType
(
InputType
.
TYPE_NULL
)
;
}
private
void
setDateTimeField
(
)
{
fromDateEtxt
.
setOnClickListener
(
this
)
;
toDateEtxt
.
setOnClickListener
(
this
)
;
Calendar
newCalendar
=
Calendar
.
getInstance
(
)
;
fromDatePickerDialog
=
new
DatePickerDialog
(
this
,
new
OnDateSetListener
(
)
{
public
void
onDateSet
(
DatePicker
view
,
int
year
,
int
monthOfYear
,
int
dayOfMonth
)
{
Calendar
newDate
=
Calendar
.
getInstance
(
)
;
newDate
.
set
(
year
,
monthOfYear
,
dayOfMonth
)
;
fromDateEtxt
.
setText
(
dateFormatter
.
format
(
newDate
.
getTime
(
)
)
)
;
}
}
,
newCalendar
.
get
(
Calendar
.
YEAR
)
,
newCalendar
.
get
(
Calendar
.
MONTH
)
,
newCalendar
.
get
(
Calendar
.
DAY_OF_MONTH
)
)
;
toDatePickerDialog
=
new
DatePickerDialog
(
this
,
new
OnDateSetListener
(
)
{
public
void
onDateSet
(
DatePicker
view
,
int
year
,
int
monthOfYear
,
int
dayOfMonth
)
{
Calendar
newDate
=
Calendar
.
getInstance
(
)
;
newDate
.
set
(
year
,
monthOfYear
,
dayOfMonth
)
;
toDateEtxt
.
setText
(
dateFormatter
.
format
(
newDate
.
getTime
(
)
)
)
;
}
}
,
newCalendar
.
get
(
Calendar
.
YEAR
)
,
newCalendar
.
get
(
Calendar
.
MONTH
)
,
newCalendar
.
get
(
Calendar
.
DAY_OF_MONTH
)
)
;
}
@
Override
public
boolean
onCreateOptionsMenu
(
Menu
menu
)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater
(
)
.
inflate
(
R
.
menu
.
main
,
menu
)
;
return
true
;
}
@
Override
public
void
onClick
(
View
view
)
{
if
(
view
==
fromDateEtxt
)
{
fromDatePickerDialog
.
show
(
)
;
}
else
if
(
view
==
toDateEtxt
)
{
toDatePickerDialog
.
show
(
)
;
}
}
}
|
-
We create an instance for SimpleDateFormat to display the date in “dd-MM-yyyy” format.
dateFormatter = new SimpleDateFormat(“dd-MM-yyyy”, Locale.US); - The following code sets on click listener for EditText.
|
1
2
|
fromDateEtxt
.
setOnClickListener
(
this
)
;
toDateEtxt
.
setOnClickListener
(
this
)
;
|
- We declare two instances of DatePickerDialog – fromDatePickerDialog, toDatePickerDialog – by using the constructor DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth).
- We implement the DatePickerDialog.OnDateSetListener interface to receive a callback when the user sets the date. In the onDateSet() callback we set the date selected by the user in the EditText (using SimpleDateFormat “dd-MM-yyyy” format).
|
1
|
fromDateEtxt
.
setText
(
dateFormatter
.
format
(
newDate
.
getTime
(
)
)
)
;
|
- We display the date picker dialog by calling show() method on dialog instance in onClick listener of EditText.
|
1
2
3
4
5
6
7
8
|
@
Override
public
void
onClick
(
View
view
)
{
if
(
view
==
fromDateEtxt
)
{
fromDatePickerDialog
.
show
(
)
;
}
else
if
(
view
==
toDateEtxt
)
{
toDatePickerDialog
.
show
(
)
;
}
}
|
Output
-
Bishal
-
http://my.opera.com/sufian88/blog/ Sufian
-
http://qaleps.tumblr.com Alejandro Lagos
-
http://my.opera.com/sufian88/blog/ Sufian
-
-
http://my.opera.com/sufian88/blog/ Sufian
-
Madhu
-
-
Sean Baergen
-
nidamanuri chandu
-
-
Harshad
-
http://themasterworld.com/ The Master World
-
http://themasterworld.com/ The Master World
-
http://themasterworld.com/ The Master World
-
VIJAY T
-
Krishna Chandramouli
-
Derrick Njenga
-
Gokul G
-
android developer
-
abderrahim zaghouri
-
fayzal
-
Rakshit
-
Desarrollo Escuintla
-
Parimal Debbarma
-
Ankit Palli
-
Ankit Palli
-
L.H.B L.H.B
-
greenkraftz