Wednesday, 12 November 2014

Simple Date Picker

Simple Date Picker

Hi!

In android it is easy to call the default date picker

we have to custom the DialogFragment

// declare year,month date as integer

        private int mYear;
private int mMonth;
private int mDay;

Button btn_dob;

btn_dob = (Button) findViewById(R.id.btn_reg_dob);

btn_dob.setOnClickListener(this);

switch (v.getId()) {
case R.id.btn_reg_dob:
                       // call the custom DialogFragment Class on click event
DialogFragment fragments = new DOBSelectDateFragment();
fragments.show(getFragmentManager(), "Date Picker");
break;
}

// customize the DialogeFragment for Date Picker
class DOBSelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

public Dialog onCreateDialog(Bundle savedInstanceState) {

return new DatePickerDialog(getActivity(), this, mYear, mMonth,
mDay);
}

@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;

onPopulateSet(year, monthOfYear + 1, dayOfMonth);

}

private void onPopulateSet(int year, int i, int dayOfMonth) {
                        // Set the result wherever you want
et_reg_dob.setText(dayOfMonth + "/" + i + "/" + year);
}
}

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

No comments:

Post a Comment