I am trying to create a DatePickerDialog in my app in Android but when I create a DatePickerDialog I receive the following message: Call requires API level 24 (current min is 14): android.app.DatePickerDialog#DatePickerDialog
How can I use a DatePickerDialog in old API versions?
Use one of the constructors added in API level 1, not one of those added in API level 24.
Here is an example that works from API level 1:
val cal = Calendar.getInstance() // used for initializing the date picker
val datePickerDialog = DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener { datePicker, y, m, d ->
// get user's selected y, m and d here
},
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
)
datePickerDialog.show()
Related
I need a help.
I'm working with date picker material.io.
And I get different format while installed the APK on difference device.
I figure out the problem is about language device setting.
Maybe someone here has the same problem like me? How to fix this problem?
Here's my date picker code :
private val datePicker = MaterialDatePicker.Builder.datePicker().build()
with(datePicker) {
addOnCancelListener { datePicker.dismiss() }
addOnPositiveButtonClickListener {
val formatter = DateTimeFormatter.ofPattern("MMM d, yyyy")
datePickerValue = LocalDate.parse(datePicker.headerText, formatter)
binding.rowInputSearchDob.edtDob.setText(datePickerValue withFormat getString(R.string.date_format_d_MMM_yyyy))
}
}
Thanks before :)
Your date formatter and parser will be using the format and language settings of the device.. the "default" locale.
To override this you need to specify the locale. Probably by using ".withLocale" see withLocale docs
i'm using DatePickerDialog to select time on my app. When I run my app on a device with Android 5 as OS the dialog shows properly, meanwhile if i run my app on a device with Android 6 this is what the dialog looks like:
Could someone help me?
I solved the issue changing the way that the TimePickerDialog was being created.
I had this code :
Calendar c = Calendar.getInstance();
return new TimePickerDialog(getContext(), myTimeListener, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
I changed the 2nd line for this one:
Calendar c = Calendar.getInstance();
return new TimePickerDialog(getContext(), R.style.AppTheme, myTimeListener, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
I am using DatePickerDialog in Android to select Date range, I am able to set the minDate and MaxDate in LolliPop (5+). But while executing the same in below Lollipop it is throwing exception.
Code Used:
Calendar cal_currentDate=Calendar.getInstance();
cal_currentDate.set(cal_currentDate.get(Calendar.YEAR),cal_currentDate.get(Calendar.MONTH)+1,cal_currentDate.get(Calendar.DAY_OF_MONTH));
Calendar cal = Calendar.getInstance();
DatePickerDialog datepicker=new DatePickerDialog(getActivity(),new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {} },
cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
datepicker.show();
datepicker.getDatePicker().setMaxDate(cal.getTimeInMillis()); // here am setting Maximum date as current date
cal.add(Calendar.MONTH,-3); // here am setting Minimum date 3 months less than current date
datepicker.getDatePicker().setMinDate(cal.getTimeInMillis());
datepickerdialog.show();
Result: Working fine in Android Lollipop.
Crashing with exception in other lower versions
Here is the exception Message
java.lang.IllegalArgumentException: Time not between Sat Mar 21 18:23:16 GMT+00:00 2015 and Tue Apr 21 18:23:16 GMT+00:00 2015
at android.widget.CalendarView.goTo(CalendarView.java:789)
at android.widget.CalendarView.setMinDate(CalendarView.java:478)
at android.widget.DatePicker.setMinDate(DatePicker.java:316)
at com.teledna.moby.ui.Fragment_homeMenu$16.onCheckedChanged(Fragment_homeMenu.java:1124
Please help...
setMaxDate
doesn't work below Honeycomb.
Extend the DatePickerDialog class to support Android versions earlier than API level 11.
Check the link:
How to make a Custom DatePicker control with maximum and minimum date range in Android application
OK, so my code has worked good until the day became Sunday.
I'm working on an app that uses the Calendar util allot, so it functioning the way i think it does is important to me! The problem:
import java.util.Calendar;
...
Calendar test = Calendar.getInstance();
test.setFirstDayOfWeek(Calendar.MONDAY);
Log.e("WEEEK TEST:", ""+ test.get(Calendar.WEEK_OF_YEAR));
test.add(Calendar.WEEK_OF_YEAR, 1);
Log.e("WEEEK TEST:", ""+ test.get(Calendar.WEEK_OF_YEAR));
Outputs this:
06-01 14:04:07.636 12005-12005/test.app E/WEEEK TEST:﹕ 23
06-01 14:04:07.636 12005-12005/test.app E/WEEEK TEST:﹕ 23
How can this even happen, and how do i fix it?
Calendar test = Calendar.getInstance();
test.add(Calendar.WEEK_OF_YEAR, -1);
test.add(Calendar.WEEK_OF_YEAR, 1);
test.setFirstDayOfWeek(Calendar.MONDAY);
Now "test" should work correctly
I'm using following code in my app:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
String itemTime = String.format(
Locale.getDefault(),
"%1$tA, %1$te. %1$tB %1$tY",
tmpCalendar);
German is the default language on my device, so I'd expect to get something like:
Dienstag, 7. Juni 2011
Instead I'm getting:
3, 7. 6 2011
If I use Locale.US instead of Locale.getDefault() everything works fine. Am I doing something wrong?
Oddly, it works in an emulator running Android 2.2 in German, but not on a HTC Desire, also running 2.2. Why?
It seems to be a bug of some vendors and is issue #9453 in the Android issue tracker.
SimpleDateFormat sdf = new SimpleDateFormat("EE/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);
Read more on it here
It does exactly what you ask it to do. If you want to have fine-grain control over the process, you can always use SimpleDateFormat. However, I would rather recommend this method:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
formatter.setTimeZone(TimeZone.getDefault());
String itemTime = formatter.format(tmpCalendar.getTime());
Also, I would recommend using DateFormat.DEFAULT but you expect long date format, so...