Reputation:
In my app I have to show 4-5 datepickers and set date how to do this using datepicker Dialog fragment ?
currently i have to make separate datepicker for all datepicker dialogs...
public static class RegistrationDatePicker extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mTextView.setText(String.valueOf(month + "/" + day + "/" + year));
}
}
Upvotes: 1
Views: 1636
Reputation: 2898
Since I recently had to use multiple DatePicker DialogFragment in the same activity, I wanted to share my answer.
At first here is my DatePicker DialogFragment, implemented with an interface:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener{
// This is the listener for the activity
private DatePickerFragmentListener listener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the current date to start the DatePicker with
final Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
listener.onDateSet(year, month, dayOfMonth);
}
// This is the method from the DatePicker fragment to implement in the Main Activity
public interface DatePickerFragmentListener{
public void onDateSet(int year, int month, int day);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (DatePickerFragmentListener) context;
}
}
Now the MainActivity that implements the DatePickerFragmentListener:
public class MainActivity extends AppCompatActivity
implements DatePickerFragment.DatePickerFragmentListener{
Button myButton1, myButton2;
FragmentManager fm = getSupportFragmentManager();
int DATE_DIALOG = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton1 = (Button) findViewById(R.id.myButton2);
myButton2 = (Button) findViewById(R.id.myButton3);
// SELECT A START DATE
myButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DATE_DIALOG = 1;
openDialog();
}
});
// SELECT A STOP DATE
myButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DATE_DIALOG = 2;
openDialog();
}
});
}
// I always show the same DatePicker fragment
public void openDialog(){
DatePickerFragment datepickDialog = new DatePickerFragment();
datepickDialog.show(fm, "Start Date");
}
@Override
public void onDateSet(int year, int month, int day) {
//Depending on the DATE_DIALOG key, I update the text of the corresponding button
if(DATE_DIALOG ==1){
myButton2.setText( Integer.toString(day) + "/" +
Integer.toString(month+1)+ "/" +
Integer.toString(year));
}
else if(DATE_DIALOG ==2){
myButton2.setText( Integer.toString(day) + "/" +
Integer.toString(month+1)+ "/" +
Integer.toString(year));
}
}
}
Upvotes: 5
Reputation: 1449
Yes you can use single DialogFragment for Multiple DatePicker with help of interface
You have to create interface in your custom DialogFragment in your case RegistrationDatePicker like below:
public class CustomDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public interface InterfaceCommunicator {
void sendRequestCode(int Code);
}
String activityCode;
Button btn;
public CustomDialog(){
super();
}
@SuppressLint("ValidFragment")
public CustomDialog(String activityCode, Button btn){
super();
this.activityCode = activityCode;
this.btn = btn;
}
InterfaceCommunicator interfaceCommunicator;
@NonNull
@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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
btn.setText(String.valueOf(month + "/" + day + "/" + year));
interfaceCommunicator.sendRequestCode(1);
}
@Override
public void onAttach(Activity activity) {
interfaceCommunicator = (InterfaceCommunicator) activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
interfaceCommunicator = null;
super.onDetach();
}
}
public class YourActivity extends AppCompatActivity implements CustomDialog.InterfaceCommunicator{
Button testBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
ButterKnife.bind(this);
testBtn = (Button) findViewById(R.id.testBtn);
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomDialog d = new CustomDialog("Test", testBtn);
d.show(getSupportFragmentManager(), "TestDialog");
}
});
}
@Override
public void sendRequestCode(int Code) {
if (Code == 1){
Toast.makeText(this, "Date Set", Toast.LENGTH_SHORT).show();
}
}
}
Hope this may help you
Upvotes: 0