I want to embed a DatePickerDialog
inside an Android activity itself. I do not want the DatePicker
to appear when clicked on a button as a dialog but embed it inside my activity itself. I have tried using the DatePicker
view inside my layout, but this seems to be different from DatePickerDialog
.
I am using DialogFragment
for creating the DatePickerDialog
and using the following code in onCreate
of MainAcitivity.java
FragmentTransaction ft = getFragmentManager().beginTransaction();
DatePickerDialogFragment datePickerDialogFragment = DatePickerDialogFragment.newInstance();
ft.add(R.id.main_screen_layout, datePickerDialogFragment); // main_screen_layout is given as the id for the layout related to MainActivity
ft.commit();
// DatePickerDialogFragment.java
public class DatePickerDialogFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public static DatePickerDialogFragment newInstance() {
return new DatePickerDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_datepicker, container, false);
View tv = v.findViewById(R.id.tv);
((TextView)tv).setText("This is a new instance");
return v;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
return new DatePickerDialog(getActivity(), this,
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
}
}
How to go about this?
I've DatePicker and DateTime within the activity layout. The idea is to allow the edition of both, compose a date time value and return it once the user clicks on a done/set button. Below, you can find some code showing how the DatePicker is embedded into the layout and the activity code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<DatePicker
android:id="@+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:calendarViewShown="false" />
</LinearLayout>
public class DateSelectionActivity extends Activity {
private DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
this.datePicker = (DatePicker) this.findViewById(R.id.date_value);
this.datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), null);
// ...
}
private OnClickListener confirmButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Get user selection
// DateSelectionActivity.this.datePicker.getYear());
// DateSelectionActivity.this.datePicker.getMonth());
// DateSelectionActivity.this.datePicker.getDayOfMonth());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With