Created
November 10, 2014 04:29
-
-
Save Jawnnypoo/315dbafbc257f59b6264 to your computer and use it in GitHub Desktop.
Revised example to support fragments, from http://androidexample.com/In_this_example_creating_a_date_picker_to_pick_day__month_year_of_date/index.php?view=article_discription&aid=89&aaid=113
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jawnnypoo.example; | |
import android.app.Activity; | |
import android.app.DatePickerDialog; | |
import android.app.Dialog; | |
import android.app.DialogFragment; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.DatePicker; | |
import android.widget.TextView; | |
import java.util.Calendar; | |
public class MainActivity extends Activity { | |
private TextView Output; | |
private Button changeDate; | |
private DatePickerFragment mDatePickerFragment; | |
private int year; | |
private int month; | |
private int day; | |
private static final String TAG_DATE_PICKER = "DATE_PICKER"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Output = (TextView) findViewById(R.id.Output); | |
changeDate = (Button) findViewById(R.id.changeDate); | |
// Get current date by calender | |
final Calendar c = Calendar.getInstance(); | |
year = c.get(Calendar.YEAR); | |
month = c.get(Calendar.MONTH); | |
day = c.get(Calendar.DAY_OF_MONTH); | |
mDatePickerFragment = new DatePickerFragment(); | |
mDatePickerFragment.setOnDateSetListener(pickerListener); | |
// Show current date | |
Output.setText(new StringBuilder() | |
// Month is 0 based, just add 1 | |
.append(month + 1).append("-").append(day).append("-") | |
.append(year).append(" ")); | |
// Button listener to show date picker dialog | |
changeDate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// On button click show datepicker dialog | |
mDatePickerFragment.show(getFragmentManager(), TAG_DATE_PICKER); | |
} | |
}); | |
} | |
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { | |
// when dialog box is closed, below method will be called. | |
@Override | |
public void onDateSet(DatePicker view, int selectedYear, | |
int selectedMonth, int selectedDay) { | |
year = selectedYear; | |
month = selectedMonth; | |
day = selectedDay; | |
// Show selected date | |
Output.setText(new StringBuilder().append(month + 1) | |
.append("-").append(day).append("-").append(year) | |
.append(" ")); | |
} | |
}; | |
public static class DatePickerFragment extends DialogFragment | |
implements DatePickerDialog.OnDateSetListener { | |
private DatePickerDialog.OnDateSetListener mListener; | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// Use the current date as the default date in the picker | |
final Calendar c = Calendar.getInstance(); | |
int year = c.get(Calendar.YEAR); | |
int month = c.get(Calendar.MONTH); | |
int day = c.get(Calendar.DAY_OF_MONTH); | |
// Create a new instance of DatePickerDialog and return it | |
return new DatePickerDialog(getActivity(), this, year, month, day); | |
} | |
public void onDateSet(DatePicker view, int year, int month, int day) { | |
// Do something with the date chosen by the user | |
if (mListener != null) { | |
mListener.onDateSet(view, year, month, day); | |
} | |
} | |
public void setOnDateSetListener(DatePickerDialog.OnDateSetListener onDateSetListener) { | |
mListener = onDateSetListener; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment