I'm trying to have a DatePicker dialog display when a user clicks on a button that says "Set your birthday", and what I need then is for the button to then display the user's selection back in the button. I have tried some of the solutions listed on this site with a TextView, but all to no avail. The DatePicker is displaying just fine, but I can't get the data to show up on the Button. I've tried these solutions: DatePicker not updating Textview in Android Display datepickers value in a textview Android: DatePicker and DatePicker Dialog How to transfer the formatted date string from my DatePickerFragment?
Here's my code:
First the DatePickerFragment:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
TheListener listener;
public interface TheListener {
public void returnDate(String date);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) {
listener.returnDate(formattedDate);
}
}
}
Now, the Fragment that is calling it:
public class SignUpFragment extends Fragment implements MyBirthday.TheListener {
private Button btnBirthday;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
btnBirthday = (Button) getActivity().findViewById(R.id.signup_birthday_button);
btnBirthday.setText("Set your birthday");
btnBirthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
@Override
public void returnDate(String date) {
btnBirthday.setText(date);
}
}
Any and all help will be greatly appreciated. If this is a duplicate question, I'm sorry for that, but I couldn't find a way to get my program to behave in the way I would like for it to. I couldn't find a solution on here.
Thanks in advance.