1

I have created datePicker dialog in Java file. This is a code:

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);
    //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

    // 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) {

        mDateDisplay .setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        // set selected date into datepicker also
    }
    }

I want to add ID to my DatePicker. How can I do it from Java file.

  • Read this : http://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically – Alexis C. Jun 09 '13 at 12:50

2 Answers2

0

Check it:

static final int DATE_DIALOG_ID = 999;
showDialog(DATE_DIALOG_ID);

override the following:

protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
}

Also I need a Listner:

    private DatePickerDialog.OnDateSetListener datePickerListener 
= new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;

 //set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
   .append("-").append(day).append("-").append(year)
   .append(" "));

// set selected date into datepicker also
dpResult.init(year, month, day, null);

}
};
Nizam
  • 5,698
  • 9
  • 45
  • 57
0

Its always good to take a look at android API reference. Take a look at this and this

Anyway as an answer you can do the following

DatePicker picker=datePickerDialog.getDatePicker();
picker.setId("picker");
Dulanga
  • 1,326
  • 7
  • 15