Able to choose future date when setting max date in datepicker - android

I'm targeting for and building with API level 21, using AppCompat v21.
It gives me a nicely looking new date picker, which has the unexpected property of allowing me to choose a future date when max date has been set using
datePicker.setMaxDate(Calendar.getInstance().getTimeInMillis())
The future dates are greyed out, but I can still choose any one of them. Is that a bug? Am I doing it wrong? Is there a way to prevent the user from being able to pick a future date in the date picker?
The old Holo date picker did not allow picking a future date when setting a maximum date.
UPDATE:
While it is not working properly on my Nexus 4 running stock 5.0, it is working properly on my Nexus 6 running stock Android 5.1.1. Perhaps it was a bug in Android 5.0 and it was fixed in 5.1? Can anyone confirm?

So to answer my own question:
I've looked at the Android source of DatePickerCalendarDelegate.java at grepcode, specifically at public void setMaxDate(long maxDate) for both Android versions 5.0 and 5.1.
What's new in 5.1 in setMaxDate() is that
mDayPickerView.goTo(getSelectedDay(), false, true, true);
has been changed to:
mDayPickerView.setMaxDate(maxDate);
It seems they fixed it there, which corresponds with my observation that it works as expected in 5.1 but not in 5.0.
And so it seems we're stuck with dealing with the fact that it doesn't work properly in Android 5.0 (days after max date are greyed out but can still be chosen).

I am using this and its working correctly
Call this function to open date picker
public void openDatePicker() {
DatePickerDialog dpd = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
dpd.show();
}
Here is the mDateSetListener
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
date.setText(dayOfMonth + " / " + (monthOfYear + 1) + " / " + year); // Here date is a TextView to display date
date.setVisibility(View.VISIBLE);
}
};

You can use a method in which u can pass the value of the date entered by the user and compare it for example to the current date , if it's a future date u can reset the value of date used to a specific one (for example the current date) and show a toast to inform him.

you can work around it on your CSS by finding the right element (with a "disabled" class) and setting a pointer events to none.`
.bs-datepicker-body table td.disabled {
pointer-events: none;
}

Related

timepicker selected value text size [duplicate]

This question already has an answer here:
Implement iOS like time picker in Android
(1 answer)
Closed 5 years ago.
I want to change the selected text value size of a time picker.
Do we have any libraries for the same?
As I searched we do not have any customization in android. Need to develop a UI similar to iOS time picker.
For android brucetoo/PickView similar to IOS,
This is a helper lib for us to pick date or province like IOS system WheelView widget.
Added feature to pick time with WheelView Widget
Dependencies
compile 'com.brucetoo.pickview:library:1.2.3'
to use
DatePickerPopWin pickerPopWin = new DatePickerPopWin.Builder(MainActivity.this, new DatePickerPopWin.OnDatePickedListener() {
#Override
public void onDatePickCompleted(int year, int month, int day, String dateDesc) {
Toast.makeText(MainActivity.this, dateDesc, Toast.LENGTH_SHORT).show();
}
}).textConfirm("CONFIRM") //text of confirm button
.textCancel("CANCEL") //text of cancel button
.btnTextSize(16) // button text size
.viewTextSize(25) // pick view text size
.colorCancel(Color.parseColor("#999999")) //color of cancel button
.colorConfirm(Color.parseColor("#009900"))//color of confirm button
.minYear(1990) //min year in loop
.maxYear(2550) // max year in loop
.showDayMonthYear(true) // shows like dd mm yyyy (default is false)
.dateChose("2013-11-11") // date chose when init popwindow
.build();

TimePickerDialog not showing AM/PM button in Gingerbread using SDK v22

I've recently begun updating my app to use appcompat-v22. One thing I've noticed is that on a Gingerbread emulator, the timepicker dialog is missing the AM/PM button.
I can click the empty space (inside the red circle in the below picture), and it will alternate between AM/PM, but I still can't see the actual button on the dialog.
Is this a bug in the SDK / compatability libraries, or maybe I did something horribly wrong when creating the dialog?
Edit: I didn't originally include my code, as it is very simple, I am not editing dialog themes at all. Note that it works as expected when using SDK v21 as the target.
TimePickerDialog timePickerDialog = new TimePickerDialog(adapter.getContext(), new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// do stuff
}
}), reminderItem.hour, reminderItem.minute, false);
timePickerDialog.setTitle(R.string.createReminder);
timePickerDialog.show();
Fixed in Support Library 22.1.0

How To Show The Current Time in TextView in Widget

I want to show the current time in updatable textView
I can't find the true way to make it
I want it to be updated every minute
Please anyone can help me ?
Thanks
Calendar cal = Calendar.getInstance();
SimpleDateFormat oSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");
String MyString= oSimpleDateFormat.format(cal.getTime());
EditText oEditText =(EditText) findViewById(R.id.MyEdit);
oEditText.setText(MyString);
try this
You should use ScheduledThreadPoolExecutor.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
use the digital clock tag Digital Clock built in android studio 2.1.
Here it is built in tag to show time
< Digital Clock > don't use space b/w digital and clock, it was having problem while posting the answer.

Android Material Design Datepicker with AppCompat

I'm trying to add the new Android 5.0 Material Design Datepicker to my pre 5.0 application using AppCompat. I've added
compile "com.android.support:appcompat-v7:21.0.0"
to my build.gradle file and updated my Theme to:
<?xml version="1.0" encoding="utf-8"?>
<style name="AppTheme.Base" parent="#style/Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
but the Datepicker still looks like this:
And not like this:
Can anybody tell me how to get the new datepicker to work on pre 5.0 devices?
Thanks in advance.
Update:
As well pointed out by jfcartier, there's now also MaterialDateTimePicker. It's probably a nicer solution than the one below since it has a nice themable API.
You could try the android-betterpickers library. It has a CalendarDatePickerDialog widget that looks like the one you want. It provides a light and a dark theme, but for customizing colors you'd have to add it as a library project and change the code yourself.
Usage is pretty straightforward once you add the library to your project.
// Create date picker listener.
CalendarDatePickerDialog.OnDateSetListener dateSetListener = new CalendarDatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(CalendarDatePickerDialog dialog, int year, int monthOfYear, int dayOfMonth) {
// Set date from user input.
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, 9);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.YEAR, year);
date.set(Calendar.MONTH, monthOfYear);
date.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// Do as you please with the date.
}
};
// Create dismiss listener.
CalendarDatePickerDialog.OnDialogDismissListener dismissListener = new CalendarDatePickerDialog.OnDialogDismissListener() {
#Override
public void onDialogDismiss(DialogInterface dialoginterface) {
// Do something when the user dismisses the dialog.
}
};
// Show date picker dialog.
CalendarDatePickerDialog dialog = new CalendarDatePickerDialog();
dialog.setOnDateSetListener(dateSetListener);
dialog.setOnDismissListener(dismissListener);
dialog.setThemeDark(false);
dialog.show(getSupportFragmentManager(), "DATE_PICKER_TAG");
The end result should look like this (sorry for the poor quality).
Material components is the recommended way for date picker
With the Material Components for Android you can use the new MaterialDatePicker.
add the following to your build.gradle
implementation 'com.google.android.material:material:1.1.0'
For kotlin
val builder = MaterialDatePicker.Builder.datePicker()
val picker = builder.build()
picker.show(parentFragmentManager, "date_picker_tag")
For Java
MaterialDatePicker.Builder<Long> builder =
MaterialDatePicker.Builder.datePicker();
MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
It supports the following configurations
portrait - mode date picker
landscape - mode date picker
date range picker
Mobile input picker
Additional reference
Here is the official design guideline
A complete demo app can be found here
I liked this library. It's a clone of Flavien Laurent Date and Time Picker with some improvements. Both of them are based on Official Google Date and Time Picker for Android 4.3+ but adapted for Android 2.1+.

Android: TimePicker setIs24HourView not working

I am trying to use the TimePicker in the 24 hour format and I am using setIs24HourView= true, but I am still not getting 24 hour format on the TimePicker. Here is my code in the onCreate of the Activity.
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
I even tried timePicker.setCurrentHour(cal.HOUR_OF_DAY) after setIs24HourView, but I am not able to see the 24 hour format on the TimePicker.
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110dp" />
Am I missing anything here?
ok, I see what the problem is now.
It's not correctly setting the currentHour to the new 24 hour format, which is why you got 9:05 instead of 21:05. I'm guessing it isn't 9am where you are!
It is a bug, as mentioned in this question
seems the only work around, for now, is to do something like:
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY));
I see you tried cal.HOUR_OF_DAY, but didn't provide the code you actually used, but try this and see if it helps.
I notice this problem in jelly bean You can set the time to show in 24 hour view
TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
picker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
int h = calendar.get(Calendar.HOUR_OF_DAY);
int m = calendar.get(Calendar.MINUTE);
picker.setCurrentHour(h);
picker.setCurrentMinute(m);
You can just use the device settings and thus let the user make this decision;
String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);
setting timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY)) solved the problem
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
We need to add this:
Calendar calendar = Calendar.getInstance();
timePicker.setCurrentHour(calendar.get(calendar.HOUR_OF_DAY));

Categories

Resources