Date Picker Dialog and Time Picker Dialog in Android use to select a date and time, as it will be popup when you needed. While Time Picker and Date Picker widget occupy more space on screen. Dialogs are best way to solve this issue.
In this tutorial, we are going to demonstrate the use of Date Picker through DatePickerDialog and TimePicker through TimePickerDialog.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).
2. First create a simple layout as shown in video. The layout contains two buttons and two hidden textviews. The layout name is activity_main.xml.
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 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/selectdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="Select Date" /> <TextView android:id="@+id/set_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" /> <Button android:id="@+id/selecttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="Select Time" /> <TextView android:id="@+id/set_time" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" /> </LinearLayout> |
3. Now add the following code to your MainActivtiy.java. In this code i showed how to display the time and date dialog box and get the selected date and time from dialog and show on textViews.
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 92 93 94 95 96 97 98 99 100 101 |
package com.date_n_time_picker_demo; import java.util.Calendar; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends AppCompatActivity { private static Button date, time; private static TextView set_date, set_time; private static final int Date_id = 0; private static final int Time_id = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); date = (Button) findViewById(R.id.selectdate); time = (Button) findViewById(R.id.selecttime); set_date = (TextView) findViewById(R.id.set_date); set_time = (TextView) findViewById(R.id.set_time); date.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // Show Date dialog showDialog(Date_id); } }); time.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // Show time dialog showDialog(Time_id); } }); } protected Dialog onCreateDialog(int id) { // Get the calander Calendar c = Calendar.getInstance(); // From calander get the year, month, day, hour, minute int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); switch (id) { case Date_id: // Open the datepicker dialog return new DatePickerDialog(MainActivity.this, date_listener, year, month, day); case Time_id: // Open the timepicker dialog return new TimePickerDialog(MainActivity.this, time_listener, hour, minute, false); } return null; } // Date picker dialog DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { // store the data in one string and set it to text String date1 = String.valueOf(month) + "/" + String.valueOf(day) + "/" + String.valueOf(year); set_date.setText(date1); } }; TimePickerDialog.OnTimeSetListener time_listener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hour, int minute) { // store the data in one string and set it to text String time1 = String.valueOf(hour) + ":" + String.valueOf(minute); set_time.setText(time1); } }; } |
4. That’s all – run the app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.
2 Comments
JAWAHER RAVICHANDRAN
Monday, November 12th, 2018its not workinggg
Dr. Droid
Monday, November 12th, 2018Hi Jawaher,
Can you tell me what issue you are facing?
Thanks