Reputation: 41
How can I show Date picker and time picker in the same dialog box ? If I' using
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
what should be done on MainAcitiviy.java I'm new to android. I searched but didnt get anything,,
Upvotes: 1
Views: 7109
Reputation: 33
You can get both date and time from single dialog.
Use following Code:
new SingleDateAndTimePickerDialog.Builder(getActivity())
// .bottomSheet()
// .curved() .title("Select Date")
.titleTextColor(getResources().getColor(R.color.white))
.minutesStep(1)
.backgroundColor(getResources().getColor(R.color.smokewhite))
.mainColor(getResources().getColor(R.color.colorAccent))
.listener(new SingleDateAndTimePickerDialog.Listener()
{
@Override public void onDateSelected(Date date)
{
// selectpackages();
String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
// String DATE_FORMAT_NOW1 = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
// SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_NOW1);
stringDate = sdf.format(date);
getDateDifference(stringDate);
// Toast.makeText(getActivity(),""+stringDate,Toast.LENGTH_SHORT).show();
}
}).display();
Use this dependency:
compile 'com.github.florent37:singledateandtimepicker:1.1.0'
Upvotes: 2
Reputation: 5287
Here is a good tutorial to start : http://www.androidapplicationdevelopment.guru/2014/06/custom-dialog-box-in-android-with-example.html
Just follow what he's doing, but put your layout with the TimePicker and DatePicker instead of his dialog.xml (rewrite it so that it doesn't display a kitty cat but your pickers)
Hope this helps!
Upvotes: 0
Reputation: 119
You can put both Date Picker and Time Picker in a layout XML: Referring this date_time_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />
<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
The code
final View dialogView = View.inflate(activity, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
time = calendar.getTimeInMillis();
alertDialog.dismiss();
}});
alertDialog.setView(dialogView);
alertDialog.show();
Upvotes: 4