Monday, December 19, 2011

Android Date Picker Example

Android Date Picker Example

android sample and simple example for date picker.

creating date picker dialog.

  @Override
     protected Dialog onCreateDialog(int id) {
         switch (id) {
         case DATE_DIALOG_ID:
             return new DatePickerDialog(this,
                         mDateSetListener,
                         mYear, mMonth, mDay);
         }
         return null;
     }

date set in date picker
private DatePickerDialog.OnDateSetListener mDateSetListener =
     new DatePickerDialog.OnDateSetListener() {

         public void onDateSet(DatePicker view, int year,
                               int monthOfYear, int dayOfMonth) {
             mYear = year;
             mMonth = monthOfYear;
             mDay = dayOfMonth;
             updateDisplay();
         }
     };

updated date in text view;

private void updateDisplay() {
        tx.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mDay).append(" "));
    }

calling date picker dialog in button onclick method

datePick.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
           
        }
    });

sample screen


full activity code;

public class DatePickerExampleActivity extends Activity {
    /** Called when the activity is first created. */
    TextView tx;
   
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      tx=(TextView)findViewById(R.id.date);
      Button datePick=(Button)findViewById(R.id.button1);
      final Calendar c = Calendar.getInstance();
      mYear = c.get(Calendar.YEAR);
      mMonth = c.get(Calendar.MONTH);
      mDay = c.get(Calendar.DAY_OF_MONTH);
      updateDisplay();
     
      datePick.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
           
        }
    });
    }
   
      private void updateDisplay() {
        tx.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mDay).append(" "));
    }

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

         public void onDateSet(DatePicker view, int year,
                               int monthOfYear, int dayOfMonth) {
             mYear = year;
             mMonth = monthOfYear;
             mDay = dayOfMonth;
             updateDisplay();
         }
     };
    
     @Override
     protected Dialog onCreateDialog(int id) {
         switch (id) {
         case DATE_DIALOG_ID:
             return new DatePickerDialog(this,
                         mDateSetListener,
                         mYear, mMonth, mDay);
         }
         return null;
     }
}
you can download sample source code here

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...