Showing posts with label Picker. Show all posts
Showing posts with label Picker. Show all posts

Wednesday, August 1, 2012

Date Time Picker in android

We know how simple is to create Date Picker Dialog and  Time Picker Dialog ! but so what about a situation where we  need to pick Time and Date both at once with all attributes e.g AM_PM, second, minute, Hour, Month, Year etc. We got stuck  right?

I have search a lot about this on Google but i did not find efficient and easy solution. So decide to write about this important concept. While you are developing any android application then you just copy one class from my sample project. and your work has done.

There are two simple step to understand what's going on in my project

1) Create object of CustomDateTimePicker with two argument Your Current activity context and one Call back listener so that when you select time and date, it will return result

2) CustomDateTimePicker has number of method to fulfill your custom requirement like---


/**
* Pass Directly current time format it will return AM and PM if you set
* false
*/
custom.set24HourFormat(false);

                 /**
* Pass Directly current data and time to show when it pop up
*/
custom.setDate(Calendar.getInstance());

Custom is instance of my CustomDateTimePicker .Except this , CustomDateTimePicker  has number of method. 


                       The Output of my sample project will be like these screen shot.

Date time Picker
Date And Time Picker

Selected Value
          
                                                       

                                                              Download Source Code


Time Picker in android

In my previous post Date Picker in android, I discuss about how to create Date Picker now we will cover another important aspect Timepickerdialog in android. While developing android application we meet so many situation where we need to pick time. In time picker also, we have two important  steps to remember

1) Call back listener that will return Time in hour and minute 

2) And Time picker dialog object


Note - Always use overridden method onCreateDialog(int id) of activity to avoid window leak exception while changing orientation of activity

Making call back listener to Time picker Dialog


      OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {  
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Builder builder = new AlertDialog.Builder(DateAndTimePicker.this);
builder.setMessage("Selected Time : " + hourOfDay + ":" + minute);
builder.setPositiveButton("OK", null);
builder.show();
}
};

Now our time picker call back listener is ready now we just need to attach it with Time picker dialog object. In normal Time picker we do not have way to select AM and PM time for that i will post another article which include Time and date picker together

Create TimePickerDialog 

 @Override  
protected Dialog onCreateDialog(int id) {
Calendar calendar = Calendar.getInstance();
switch (id) {
case 2:
TimePickerDialog timePicker = new TimePickerDialog(this,
onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), false);
return timePicker;
}
return super.onCreateDialog(id);
}
}

Now look at the screen shot what exactly this code will brings.........

Time Picker Dialog
Time Picker
 Download source from following link              DownLoad Source Code

Related Link on this blog

How to create a custom dialog ? , How to create a custom dialog and built in dialog of all type?

Date picker in android

In android application development, picker and dialog has an important place. Today i am going to discuss how to create a simple date picker dialog and time picker dialog. All though i know i am very late to write about this basic concept, but i realize that even-though a lots of matter and article available on this but they lack in term of explanation. So here i go with my attempt.

We need two things here

1) Dialog picker (either date or time) Object

2) And call back so that we can capture date or time when user set date


We will set current date or time when picker will open first time using calender object. Creating Datepickerdialog object inside overridden method of activity ,oncreatedialog() is best way. It will handle the orientation change effectively and you will avoid window leak exception quite easily



Making a call back listener for Datepickerdialog 


      OnDateSetListener ondate = new OnDateSetListener() {  
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Builder builder = new AlertDialog.Builder(DateAndTimePicker.this);
builder.setMessage("Selected date : " + year + "/"
+ (++monthOfYear) + "/" + dayOfMonth);
builder.setPositiveButton("OK", null);
builder.show();
}
};

It will return the date when you will click select button of  Datepickerdialog. Now override onCreateDialog(int d) and return Datepickerdialog object

      @Override  
protected Dialog onCreateDialog(int id) {
Calendar calendar = Calendar.getInstance();
switch (id) {
case 1:
DatePickerDialog datePicker = new DatePickerDialog(this, ondate,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
return datePicker;
}
return super.onCreateDialog(id);
}

DatePicker in android
Date Picker View

Date Picker selected value
Selected date

Follow this link to download source   Download Source Code



                                            
Posting Lama ►
 

Copyright 2013 Tutorials For Newbie: Picker Template by CB Blogger Template. Powered by Blogger