How do you change the current string of a text on a AlertDialog
after you click a button that selects a date from a CalendarPickerFragment.
This the AlertDialog
public class FragmentInicio extends Fragment{
private static String date;
public void addDate(){
View view = (LayoutInflater.from(getActivity())).inflate(R.layout.alert_dialog,null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setView(view);
final TextView date_Text = view.findViewById(R.id.dateEditText);
final ImageButton dateButton= view.findViewById(R.id.tareaFechaButton);
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CalendarPickerFragment newFragment = new CalendarPickerFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
date_Text.setText(date);
alertDialog.setTitle("New Date");
alertDialog.setCancelable(true).setPositiveButton("Save", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some stuff
}
});
alertDialog.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog dialog = alertDialog.create();
dialog.show();
}
public static void displaydate(int year, int monthOfYear, int dayOfMonth) {
date = String.format("%02d", dayOfMonth) + "/" + String.format("%02d", monthOfYear + 1) + "/" +
String.format("%02d", year);
Log.d("DTAG", "date: "+date);
}
CalendarPickerFragment
public class CalendarPickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private CalendarPickerFragment.OnDateReceiveCallBack mListener;
private Context context;
public interface OnDateReceiveCallBack {
public void onDateReceive(int dd ,int mm, int yy);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
try {
mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
}
}
public CalendarPickerFragment(){
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, i + "/" + "/" + i1 + "/" + i2);
mListener.onDateReceive(i,i1,i2);
}
Ok so you can use the listener you have already craeted in your
alertDialog
Following should be yourCalendarPickerFragment
Now initialise this
fragmnet
as follows