I have two editText views, and when a user clicks the first editText, a dialogfragment window opens and the user selects the date. The same happens with the second editText. The date selected should be "written" in the editText.This works like a charm when I have one editText. But how do I know which ediText was clicked, as they are in the same activity and I have to edit the editText in the dialogFragment class?
I have seen some other answers, but some of them are really outdated-deprecated, and some other didn 't help me.
Here is my code:
Start class:
public class Start extends FragmentActivity {
private EditText ddEdit;
private EditText adEdit;
int year;
int month;
int day;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
public void showDatePickerDialog(View v) {
switch (v.getId()) {
//i know this is not correct
case R.id.etDepDate:
DialogFragment depFragment = new DatePick();
depFragment.show(getFragmentManager(), "datepicker1");
break;
case R.id.etArrDate:
DialogFragment arrFragment = new DatePick();
arrFragment.show(getFragmentManager(), "datepicker2");
break;
}
}
DatePick class:
public class DatePick extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
private EditText ddEdit;
private EditText adEdit;
@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) {
String date = new String(day + "/" + (month + 1) + "/" + year);
//????
//how I know here which view was clicked so I can call the editTextById method?
//??????
}
public void editTextById(View v, String date) {
switch (v.getId()) {
case R.id.etDepDate:
setDate(date, ddEdit);
setDate(date, adEdit);
break;
case R.id.etArrDate:
setDate(date, adEdit);
break;
}
}
public void setDate(String date, EditText editText) {
editText.setText(date);
}
@Override
public void show(FragmentManager manager, String tag) {
// TODO Auto-generated method stub
super.show(manager, tag);
}
}
you can pass the view when creating the dialog using newInstance(viewToBeEdited).
In the showDatePickerDialog modify your creation of the dialog in this way
public void showDatePickerDialog(View v) {
depFragment = DatePick.newInstance(v);
depFragment.show(getFragmentManager(), "datepicker1");
break;
}
Your DatePick Dialog must implement the static method newInstance(Viev v)
public class DatePick extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private EditText editableView;
public static DatePick newInstance(View v) {
DatePick dialog = new DatePick();
dialog.editableView = v;
return dialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
<Your code>
}
public void onDateSet(DatePicker view, int year, int month, int day) {
final String date = new String(day + "/" + (month + 1) + "/" + year);
getActivity().runOnUIThread(new Runnable() {
public run() {
editableView.setText(date);
}
});
}
}
I didn't test it, so there could be some typos, but this is typically what I do in these cases.