This post I write code for display DatePickerDialog using FragmentDialog.
Generally we display DatePickerDialog using "onCreateDialog (int id)" Method like below.
But now "Dialog onCreateDialog(int id)" method deprecated in API level 8.
So now we can use DialogFragment to display DatePickerDialog and this code working to all android version. I finding DatePickerDialogFragmetn.java class from here.
1. Copy DatePickerDialogFragment.java in your application package.
2. Create MainActivity.java
3. activity_main.xml
4. ScreenShot
- Jelly Bean DatePickerDialog — is there a way to cancel?
- android-android-4-1-emulator-invoking-ondateset-twice-from-datepicker-dialog
Generally we display DatePickerDialog using "onCreateDialog (int id)" Method like below.
//.....
static final int DATE_DIALOG_ID = 999;
//
//
call below code to display DatePickerDialog
showDialog(DATE_DIALOG_ID);
//
//
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
//
//
//we use DatePickerDialog.OnDateSetListener Listner to get selected date like below
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
}
};
But now "Dialog onCreateDialog(int id)" method deprecated in API level 8.
So now we can use DialogFragment to display DatePickerDialog and this code working to all android version. I finding DatePickerDialogFragmetn.java class from here.
1. Copy DatePickerDialogFragment.java in your application package.
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.widget.DatePicker;
public class DatePickerDialogFragment extends DialogFragment
{
public static final String YEAR = "Year";
public static final String MONTH = "Month";
public static final String DATE = "Day";
private OnDateSetListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mListener = (OnDateSetListener) activity;
}
@Override
public void onDetach() {
this.mListener = null;
super.onDetach();
}
@TargetApi(11)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Bundle b = getArguments();
int y = b.getInt(YEAR);
int m = b.getInt(MONTH);
int d = b.getInt(DATE);
final DatePickerDialog picker = new DatePickerDialog(getActivity(), getConstructorListener(), y, m, d);
if (hasJellyBeanAndAbove()) {
picker.setButton(DialogInterface.BUTTON_POSITIVE,
getActivity().getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatePicker dp = picker.getDatePicker();
mListener.onDateSet(dp,
dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
}
});
picker.setButton(DialogInterface.BUTTON_NEGATIVE,
getActivity().getString(android.R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
}
return picker;
}
private static boolean hasJellyBeanAndAbove()
{
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
}
private OnDateSetListener getConstructorListener() {
return hasJellyBeanAndAbove() ? null : mListener;
}
}
2. Create MainActivity.java
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends Activity implements OnDateSetListener
{
private TextView tvDisplayDate;
private Button btnChangeDate;
private int year;
private int month;
private int day;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDate();
button_clickEvent();
}
// display current date
public void setCurrentDate()
{
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
Format formatter = new SimpleDateFormat("dd-MM-yyyy");
String s = formatter.format(c.getTime());
tvDisplayDate.setText(s);
}
public void button_clickEvent()
{
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Bundle b = new Bundle();
b.putInt(DatePickerDialogFragment.YEAR, year);
b.putInt(DatePickerDialogFragment.MONTH, month);
b.putInt(DatePickerDialogFragment.DATE, day);
DialogFragment picker = new DatePickerDialogFragment();
picker.setArguments(b);
picker.show(getFragmentManager(), "fragment_date_picker");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
this.year = year;
this.month = monthOfYear;
this.day = dayOfMonth;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Format formatter = new SimpleDateFormat("dd-MM-yyyy");
String s = formatter.format(calendar.getTime());
tvDisplayDate.setText(s);
}
}
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Current Date (dd-MM-yyyy): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="asdasd"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Change Date" />
</LinearLayout>
4. ScreenShot
Download APK file Here
Reference link for new version DatePickerDialog issues.- Jelly Bean DatePickerDialog — is there a way to cancel?
- android-android-4-1-emulator-invoking-ondateset-twice-from-datepicker-dialog


did you make it on fragment with using latest way
ReplyDelete