I’ve a problem with a DatePickerDialog following steps from Android Tutorial.
Linking a TextView to launch a DatePickerDialog when gets focused as you can see below:
EditText fNac = (EditText)findViewById(R.id.regFecha);
fNac.setonfocusChangeListener(new View.onfocusChangeListener(){
@Override
public void onfocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
// Desde aquí lanzamos el datepicker
if (hasFocus){
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager() , "datePicker");
}
}
Works fine, the DatePickerDialog is launched. I instance a new DatePickerFragment class that is created in this way:
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private DatePickerDialog mDatePickerDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Usamos la fecha actual como primera fecha a mostrar
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);
// Devolvemos la instancia de un Dialog con la fecha actual
mDatePickerDialog = new DatePickerDialog(getActivity(),this,year,month,day);
return mDatePickerDialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// TODO Auto-generated method stub
Log.v("LOG_CAT","OK");
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
Log.v("LOG_CAT", "Ok2");
}
}
And here is the problem. I want to change Dialog’s title using onDateChanged method, but it’s never called. I tried to create a onDateChangedListener but DatePickerDialog doesn’t have one.
How can i do this?
Thanks in advance.
Simply extend the DatePickerDialog class and override onDateChanged like below. I included a new constructor that accepts a Calendar object and sets the year, month and day itself, saving you the unnecessary typing. (I don’t understand why this isn’t an option already.) Anyway here is how to use the class and the shorter constructor: