How to get both date and time in a single dialog box - android

I am using following code to pick date, however i wanted to pick the date as well as time (in a single dialog box)...
DatePickerDialog.OnDateSetListener beginDate = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,monthOfYear);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
setBeginDateOnView();
}
};
public void beginDateOnClick(View view) {
new DatePickerDialog(CreateEventActivity.this,beginDate,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
public void setBeginDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
edtBeginDate.setText(sdf.format(c.getTime()));

There is no built-in "date and time picker in one" solution for Android, so you'll have to combine a DatePicker and a TimePicker into a dialog by yourself, or use a library that does it.
For a library, i can suggest using SlideDateTimePicker, i used it in a project a while back and it works well.
Or you can write your own, by simply creating a new class that extends AlertDialog, setting up 2 tabs inside it and then setting the tabs to include a DatePicker and a TimePicker.

Related

DatePickerDialog display bug on Samsung galaxy S7 run Android 7.x

Only Samsung s7 run android 7.x
Here my code
DatePickerDialog datePickerDialog = new DatePickerDialog(activity
,new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year
, int monthOfYear
, int dayOfMonth) {
listener.onDateSet(year, monthOfYear, dayOfMonth);
}
}
,c.get(Calendar.YEAR)
, c.get(Calendar.MONTH)
, c.get(Calendar.DAY_OF_MONTH));
int firstDayOfWeek = TimeUtils.getFirstDayOfWeek(activity);
datePickerDialog.getDatePicker().setFirstDayOfWeek(firstDayOfWeek);
datePickerDialog.show();
Someone advise me how to solve this problem?
Thanks!
You don't set custom style for dialog so this normal(default style). If you want "fix" this "bug" you must create custom style dialog.
For me works fine on the same device, I extend DialogFrament in my app and use code like this:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
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);
// 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) {
// Do something with the date chosen by the user
}
}
and then use this method on the view I need to open the dialog:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
Try to implement this or follow the guide on the developer site:
https://developer.android.com/guide/topics/ui/controls/pickers.html
The problem is that the actual implementation of DatePickerDialog's UI is device specific and it can have some issues.
I've got similar issues on devices with the screen width smaller than 360dp. Fixed this by using THEME_HOLO_LIGHT theme.
DatePickerDialog has a constructor which accepts a theme:
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
You may use one of the following:
AlertDialog.THEME_DEVICE_DEFAULT_DARK
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
AlertDialog.THEME_HOLO_DARK
AlertDialog.THEME_HOLO_LIGHT
AlertDialog.THEME_TRADITIONAL

Show 1980 as initial Year in Date Picker

How to show 1980 as initial Year in DatePicker dialog, still i am getting initial date as current date
see my code below, and let me know where i have to do changes in my code
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
return null;
That is because the last 3 parameters on the DatePickerDialog constructor are the date to be shown initially in the DatePicker. You are setting the dateTime variable to the current day with
private Calendar dateTime = Calendar.getInstance();
So you must set your dateTime variable to the correct date you want to be shown on the first run. In your OnCreate() method do this
dateTime.set(1980, Calendar.JANUARY , 1);
Now your DatePicker should show the correct value on the first run and since you set the dateTime variable in the callback it should set the correct previous value if the user opens up the dialog again.

Trying to return a date from android date picker

Over the past 24 hours I have been trying to find articles about my issue but am still confused. If I have missed a place where this question was previously answered I apologize but I hope that you all will still help me understand what I am missing.
Synopsis: I want to populate a few text boxes with a date picker and am having trouble understanding how to return the selected date so that I can populate the appropriate textbox.
Basically I'm trying to implement a standard date picker targeting android devices >= 4.0.
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
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);
// 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) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
You will notice that this is the code from the android documentation example that can be found here: http://developer.android.com/guide/topics/ui/controls/pickers.html
I am implementing this code in a larger project and the date picker comes up fine, the date is picked fine etc.
What I am having trouble with is getting the date after the user selects it. Basically the user selects the date and then nothing happens. What I want to do is populate a specific text box with the date after selection. This will happen in a few text boxes so I can't just hard code it into the onDateSet function.
My thought was to pass the textBox's id with the creation of the datepicker but I am not completely sure how I would do that.
This is my first android project and focused on comprehension and not simply getting a working project, thanks.
The "onDateSet" method should be called when the user selects a date.
In this method, you can use the year, month and day passed in parameter.
I don't known if that is the best way to do it, but I solve it this way:
I have two buttons, A and B.
When i click button A, a DatePicker appears and the user selects the date.
After confirm the date, the text in Button A is set to that date that was selected.
Same behavior for button B.
That is the function when button A or B is pressed:
public void selectDateButtons(View view){
if(view.getId() == R.id.buttonA)
{
clickedButton = R.id.buttonA;
}else{
clickedButton = R.id.buttonB;
}
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
clickedButton is a Static global variable.
Then declare the sub-class
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
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(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if(clickedButton == R.id.ButtonA)
{
ButtonA.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
} else {
ButtonB.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
}
}
}
Don't forget, your main activity should extends FragmentActivity.
Hope that works for you too!
Best regards!

Date Picker alert Dialog

When i am running my code in android 3.2 and android 4.0.3, it shows different date pickers..i want date picker as a wheeler like 1st image..i know 2.3.3 shows old pickers..But i don't know why i am getting older one in 4.0.3 which is giving new in 3.2. It happened same with Number Picker also.
here is my code:
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
if (picker == 1) {
final long lDate = mDay;
dateStart.set(Calendar.YEAR, mYear);
dateStart.set(Calendar.MONTH, mMonth);
dateStart.set(Calendar.DATE, mDay);
Log.d("startdate", "" + dateStart);
if (lDate != -1) {
UpdateStartDateTimeView();
return;
}
}
updateDisplay();
}
};
If you use the native picker, automatically Android will choose the corresponding to the current version. You can only ensure that a unique dialog is presented regardless Android version by creating a custom Dialog. Thus, you need to see if the unique picker worths the effort of designing a custom one..
The DatePickerDialog is having a constructor which accepts a theme
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
Just update ur code with
for light holo theme
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
or
for dark holo theme
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
android.R.style.Theme_Holo__Dialog_NoActionBar,
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
If I understand you right, you're getting an old dialog style in Andoid version above 4. I had this problem when I set STYLE_NO_FRAME for dialog fragment. In this case date picker has always old design, and I don't know why it is so. If you want to get always modern UI, be sure you set STYLE_NORMAL to your dialog fragment. For do this you need to add following line in OnCreate callback method of the dialog fragment.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, 0);
}

Android Date Picker help!

i need a little help here.
In my app a user need`s select one date interval (when press one button), and i'm not know how to do this using date picker!
i'm following this example: http://developer.android.com/resources/tutorials/views/hello-datepicker.html , but i have two questions about this tutorial...
1) How can i do which onClick event called DatePickerDialog.OnDateSetListener ( in the case where i have two EditText that when user click in wich one, the app shows the Date picker Dialog)
2) How can i call the onClick two times after user press one button?
tnhx!
Sorry for poor English!
So from your explanation, you want to create a date interval picker from just one click on a button.
By using analogy from your example you have to do the following:
// create two constants
static final int DATE_DIALOG_FROM = 0;
static final int DATE_DIALOG_TO = 1;
// create case for two pickers
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_FROM:
return new DatePickerDialog(this, fromDateSetListener, mYear, mMonth,mDay);
case DATE_DIALOG_TO:
return new DatePickerDialog(this, toDateSetListener, mYear, mMonth,mDay);
}
return null;
}
// create two listeners for both of the cases
private DatePickerDialog.OnDateSetListener fromDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"From:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
showDialog(DATE_DIALOG_TO);
}
};
private DatePickerDialog.OnDateSetListener toDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"To:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
updateDisplay();
}
};
When you run your app, second picker will appear right after finishing with first.
Later you can optimize this code, add custom pickers etc...
If anyone knows a better way to create a DATE INTERVAL PICKER please let us know!

Categories

Resources