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
Related
I'm currently working on an app which has two different themes (declared in styles.xml) - light mode and dark mode. I wanted to try to get the dark mode to turn on automatically at a certain time using java.util.Calender and java.util.Date.
The if-statement I was working with was supposed to set the theme to DarkTheme after 22:00 (10pm). I was working around 1pm and I changed the code to 10, 0 and 15, 0 to test it, but the statement was somehow always true.
I got it fixed now using a different method, but I just really wanted to know why what I tried before didn't work. Maybe I'm an idiot and don't a very simple mistake I made, but logically thinking I can't find what's wrong.
(FYI, I'm working with Android Studio 3.4.1, if that would make any difference.)
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Calendar calender = Calendar.getInstance();
calender.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
if (calender.getTime().after(new Date(calender.getTime().getYear(), calender.getTime().getMonth(), calender.getTime().getDay(), 22, 0))) {
setTheme(R.style.DarkTheme);
}
else {
setTheme(R.style.LightTheme);
}
}
}
calender.getTime().getDay() returns the day of the week (I've referenced the JDK7 class, which should have the same semantics as the one in Android) and NOT the day of the month (as expected by the Date constructor). Replace this with calendar.getTime().getDate().
Is there any way to remove the AM/PM in a Time Picker Widget?
I have this function in my application but its purpose is to select only Hour and Minutes not including AM/PM, I tried to setIs24HourView(true) but it makes the time 24hours, I only want 12 hours.
It seems there is no public method in TimePicker to directly hide or show the AM/PM chooser. However, a look at the source code will give us the name of the resource ID for that View, which we can get with the system Resources. Then it's simply a matter of finding the View, and setting its visibility to GONE.
private void hideAmPmLayout(TimePicker picker) {
final int id = Resources.getSystem().getIdentifier("ampm_layout", "id", "android");
final View amPmLayout = picker.findViewById(id);
if(amPmLayout != null) {
amPmLayout.setVisibility(View.GONE);
}
}
How is it possible to show a clock when opening a TimePickerDialog on preAPI21? I'm using appcompat-22 and I even found a way to show the new switch widget. I know how to change it in a regular layout: timePickerMode="clock/spinner" but couldn't find how to do it when opening a TimePickerDialog on API 19.
Please advice
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;
}
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+.