Android datepicker update current day

private DatePickerDialog datePicker;
private TextView _my_text_view;
private Calendar _calendar = Calendar.getInstance();



@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
     
    // on start up it will show the date picker dialog.
    // you can call this method anywhere in your activitiy
    showDialog(DATE_DIALOG_ID);
}


/**
 * Create the date picker Dialog.
 */
 @Override
 protected Dialog onCreateDialog(int id) {
   switch (id) {
   case DATE_DIALOG_ID:
   
   datePicker = new DatePickerDialog (this, 
           dateSetListener, year, month, day); 
   
   return datePicker;
   }
   return null;
}


/**
 * Get the values from the datepicker.
 */
private DatePickerDialog.OnDateSetListener dateSetListener = 
         new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth){

 year = year;

 month = monthOfYear;

 day = dayOfMonth;

 updateDisplay(); 

 //Here only we are updating the current date in date picker dialog.

 datePicker.updateDate(_calendar.get(Calendar.YEAR), 
 _calendar.get(Calendar.MONTH), _calendar.get(Calendar.DATE));
  }

};

/**
 * Set Date values to the TextView.
 */
private void updateDisplay() {
  _my_text_view.setText(new StringBuilder()
 // Month is 0 based so add 1
 .append(month + 1).append("/").append(day)
 .append("/").append(year).append(""));
 }
     }
}

Comments

Popular posts from this blog

Input filter (space not allowed) for EditText

Android Versions details

English - Formal and Informal words