Hi Guys!
Today I am going to share about the code of Date Picker in Android.
This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a
For detail about the Date Picker you can visit the android developer site Date Picker.
So lets start the coding about the date picker android.
You can download the source code Date Picker
Today I am going to share about the code of Date Picker in Android.
This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a
CalendarView
. The set of spinners
and the calendar view are automatically synchronized. The client can
customize whether only the spinners, or only the calendar view, or both to be
displayed. Also the minimal and maximal date from which dates to be selected
can be customized.For detail about the Date Picker you can visit the android developer site Date Picker.
So lets start the coding about the date picker android.
activity_main.xml
1
2
3
4
5
6
7
| <linearlayout android:id= "@+id/LinearLayout1" android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" > <textview android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:text= "@string/hello_world" > <button android:id= "@+id/startdatepicker" android:layout_height= "wrap_content" android:layout_width= "fill_parent" android:text= "Start DatePicker" > </button></textview></linearlayout> |
MainAcivity.java
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
| package com.sunil.datepicker; 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.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private int year, month, day; static final int ID_DATEPICKER = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startDatePicker = (Button)findViewById(R.id.startdatepicker); startDatePicker.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); showDialog(ID_DATEPICKER); }}); } @Override protected Dialog onCreateDialog(int id) { switch (id){ case ID_DATEPICKER: return new DatePickerDialog( this , dateSetListener, year, month, day); default : return null ; } } private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(android.widget.DatePicker arg0, int arg1,int arg2, int arg3) { Toast.makeText(getBaseContext(), String.valueOf(arg1) + "/" + String.valueOf(arg2+1) + "/" + String.valueOf(arg3),Toast.LENGTH_LONG).show(); } }; } |
You can download the source code Date Picker