Friday, January 28, 2011

Android Date Picker

ANDROID DATE PICKER
DOWNLOAD SOURCE CODE
1.when ever you click datepicker ..
2.dialog window showing.you change date but cancel button u click.
3.again click the datepicker it showing date..last what u change that date only displaying.

If u don't want this issue..u have to add this code in your activity.

 @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
              
                case ID_DATEPICKER:
                    ((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
                    break;
            }
        }  

Source code:
Activity Code:
public class DatePicker extends Activity {
    private int myYear, myMonth, myDay;
    static final int ID_DATEPICKER = 0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button datePickerButton = (Button)findViewById(R.id.datepickerbutton);
        datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
    }
    private Button.OnClickListener datePickerButtonOnClickListener
        = new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Calendar c = Calendar.getInstance();
                myYear = c.get(Calendar.YEAR);
                myMonth = c.get(Calendar.MONTH);
                myDay = c.get(Calendar.DAY_OF_MONTH);
                showDialog(ID_DATEPICKER);
            }
    };
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
            case ID_DATEPICKER:
                Toast.makeText(DatePicker.this,
                        "- onCreateDialog -",
                        Toast.LENGTH_LONG).show();
                return new DatePickerDialog(this,
                        myDateSetListener,
                        myYear, myMonth, myDay);
            default:
                return null;
        }
    }
     @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
              
                case ID_DATEPICKER:
                    ((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
                    break;
            }
        }   

    private DatePickerDialog.OnDateSetListener myDateSetListener
        = new DatePickerDialog.OnDateSetListener(){
           
            @Override
            public void onDateSet(android.widget.DatePicker  view, int year,
                    int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub
                String date = "Year: " + String.valueOf(year) + "\n"
                + "Month: " + String.valueOf(monthOfYear+1) + "\n"
                + "Day: " + String.valueOf(dayOfMonth);
            Toast.makeText(DatePicker.this, date,
                    Toast.LENGTH_LONG).show();
               
            }
    };
}


1 comment:

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...