I'm experiencing a weird visual bug on new DatePickerDialog of Android widgets. I need to set a dialog's selectable date range as 1 month but it shouldn't also select a date from the future. Logic works as expected but the visuals have some sort of bug. Here is how it looks:
Note that the "5" is disabled, I cannot select it. However, it looks as selectable. What should I do to solve this issue? Any help is appreciated, thanks. (Today's day of month is also 5, I assume this bug comes from there.)
Edit: This is how I create the dialog.
#SuppressLint("ValidFragment")
public static class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private static final long MONTH = 2592000000L;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use previous selected date in the date picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(DateUtils.getAtTimeTimestamp(startDate));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_DialogTheme, this, year, month, day);
Date date = new Date(); // get current time
// get target maximum date which can be 1 month above
long targetDateTime = DateUtils.getAtTimeTimestamp(startDate) + MONTH;
if (date.getTime() < targetDateTime)
dialog.getDatePicker().setMaxDate(date.getTime()); // now because it can't be future
else
dialog.getDatePicker().setMaxDate(targetDateTime); // should also be one month above from the start date.
dialog.getDatePicker().setMinDate(DateUtils.getAtTimeTimestamp(startDate));
return dialog;
}
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
// logic between date selections
}
}
DateUtils.getAtTimeTimestamp(String date) basically converts a string to long integer that is time in milliseconds.
Related
One of my features require the user to select a month and year, so the following is the method which will pop up the first attached image (d/m/y).
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);
DatePickerDialog dp = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//Check if selected month/year is in future of today.
}
}, year, month, day);
dp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dp.setTitle("Set Monthly Overview");
dp.show();
However, i do NOT require the date. How can i disable user from selecting it, or best of all, to even not display the date option there?
What I wish to achieve is something like the following image in the best case scenario.
Refer "Android DatePicker change to only Month and Year" which will help you just display month and year in the datepicker. Hope this helps.
I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.
what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:
etSelectDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment datePickerFragment = new DatePickerFragment() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.d("Date Change", "onDateSet");
Calendar c = Calendar.getInstance();
c.set(year, month, day);
DateFormat df = DateFormat.getDateInstance();
etSelectDate.setText(df.format(c.getTime()));
//nextField.requestFocus(); //moves the focus to something else after dialog is closed
}
};
datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");
}
});
and date picker fragment class goes 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);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//blah
}
}
till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.
So please help me , How can I put limit of seven days only
Update
Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :
pickerDialog.getDatePicker().setMaxDate(new Date().getTime());
It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help
You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.
dpdialog.getDatePicker().setMinDate(minDate);
dpdialog.getDatePicker().setmaxDate(maxDate);
Source :Set Limit on the DatePickerDialog in Android?
You can calculate the minDate by using,
Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!
Updated :
Replace the below line
return new DatePickerDialog(getActivity(), this, year, month, day);
with
// Create a new instance of DatePickerDialog and return it
DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
pickerDialog.getDatePicker().setMaxDate(maxDate);
pickerDialog.getDatePicker().setMinDate(minDate);
return pickerDialog;
In some cases, when you set maximum date with a date without hours and minutes, you won't be able to select the maximum date you set.
For instance
Calendar maxDate = Calendar.getInstance();
maxDate.set(Calendar.DAY_OF_MONTH, day + 5);
maxDate.set(Calendar.MONTH, month);
maxDate.set(Calendar.YEAR, year);
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
With the code block above, you can not click to your maxDate.
But if you add hours and minutes to like;
maxDate.set(Calendar.HOUR, 23);
maxDate.set(Calendar.MINUTE, 59);
to your maxDate, your last date will be clickable
by using
setMinDate and setMaxDate
MinDate
MaxDate
try this
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date or put yore date in miliseconds.
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the min date or put your date in mili seconds format
return dpDialog;
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!
As i am new to android please help me to add calender view in dialog box on button click
I used following but it shows me scrollable day year date pattern calender
but i want to show calender like Jquery calender view which is month wise how can i show please help
by showing code thnks in advance
protected Dialog onCreateDialog( int id) {// to create Date dialog
switch (id) {
case OE_DT_DIALOG_ID:
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, yy, mm,dd);
}
return null;
}
#SuppressWarnings("unused")
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
#SuppressWarnings("deprecation")
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into btn View
lblDate.setText(new StringBuilder().append(day).append("-").append(month + 1)
.append("-").append(year).append(" "));
}
};
to show calender like this
You can do this with a Pager View. Make your pager view as horizontal scrollable and get the instance of each month and display that using a fragment. Hope this would give an idea...
Please find the help here. It is a custom library you've to link with your code
I have the following widget in my app. Where it says set date is it possible to replace this with the day of week, day of month, month and year. eg wed 07 Nov 2012?
I'm running Android 4 on htc whereas i know if i run the same code on Android 2.3.3 the the date is displayed how i want it.
.[update1]
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this,2 ,datePickerListener, year, month, day);
}
return null;
}
.[update2]
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
final DateFormat df = DateFormat.getDateInstance(0);
final Calendar mCal = Calendar.getInstance();
// Create a date picker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this,2,datePickerListener, year, month, day){
#Override
public void onDateChanged(DatePicker view, int year,int month, int day){
mCal.set(Calendar.YEAR, year);
mCal.set(Calendar.MONTH, month);
mCal.set(Calendar.DAY_OF_MONTH, day);
setTitle(df.format(mCal.getTime()));
}
};
return datePickerDialog;
//return new DatePickerDialog(this,2 ,datePickerListener, year, month, day);
}
return null;
}
Yes. I did it with following code.. You will have to change according to your requirement.
dPicker.init((previouslyEnteredDate.getYear() + 1900),
previouslyEnteredDate.getMonth(),
previouslyEnteredDate.getDate(), null);
If you just want to initialize it with the current date as a reminder of what today is (or at least don't need it to change as the user interacts with the picker) you can do it by creating a temporary variable and calling the setTitle(CharSequence) method on it (since DatePickerDialog extends AlertDialog)
for example:
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog dpd = new DatePickerDialog(this, 2, datePickerListener,
year, month, day);
dpd.setTitle("Today is: " + year + "-" + (month + 1) + "-" + day);
return dpd;
Okay, I got a bit lazy with the formatting, there, obviously, but I hope you get the basic idea.
If you want the title to update as they are changing the date on the picker, that gets more involved, but I can't imagine that's what you're going for (unless the point is to see the day of the week in context as they are changing it?). If it is, let me know and I'll elaborate.