I am using the following code to display a datepicker dialog:
DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
mDateOfJourney.set(year, monthOfYear, dayOfMonth);
if (mDateOfJourney.after(mCalendar) || mDateOfJourney.equals(mCalendar))
{
setJourneyDateView(mDateOfJourney);
} else {
Toast.makeText(getActivity(), getString(R.string.invalid_date), Toast.LENGTH_SHORT).show();
}
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(mCalendar.getTimeInMillis());
dialog.show();
The output of this code has some rendering issues: It shows up like this:
In the picture above, the window's background is null. Look through your app's style definition. You might find this somewhere:
<item name="android:windowBackground">#null</item>
Or else, you might have set the window's background to null: getWindow().setBackgroundDrawable(null).
The only theme I know that sets this attribute for you is Theme.NoDisplay. So, if you're not using this theme as a base (or as is), you will have to use a different theme.
Related
I am setting the datePickerMode in my styles file to spinner like this:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:dialogTheme">#style/MyDialogTheme</item>
<item name="android:datePickerStyle">#style/MyDatePicker</item>
</style>
<style name="ItvDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">#style/MyDatePicker</item>
</style>
<style name="MyDatePicker" parent="android:Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
and then create a DatePicker
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
//handle result
}
};
DatePickerDialog dialog = new DatePickerDialog(this, onDateSetListener, dob.getYear(), dob.getMonth() - 1,
dob.getDay());
dialog.show();
This worked fine across a lot of devices including the Samsung Galaxy S7 Edge on Android 6, but since updating the device to Android 7 it ignores the mode spinner and shows the calendar.
The spinner works fine on an One Plus 3 on Android 7.1.1.
Any ideas?
You may be running into this issue (#37120178) datepicker dialog can't switch to spinner on Android 7.0 device. If so, there is a work-around documented on that page. I haven't tried the work-around, but it may be worth a shot. (See below for implementation of the work-around.)
By the way, I have a Samsung S7 (not an edge) running 7.0. I can get a DatePicker spinner if I define it in XML, so another work-around would be to define the DatePicker spinner in XML and use it in the dialog.
Here is a gist of this solution.
Odd, but those fixes in the bug report seem to be for the time picker although the report is for the date picker. I dug around a bit and found Use DatePicker spinners in API 24 as a potential fix. Again, I have not tried this.
This reflection logic does appear to work. It showed the spinner beside the calendar view for me. If you want just the spinner, you will need to add the following to your style:
<item name="android:calendarViewShown">false</item>
(Credit for this work-around goes to lognaturel and jeffdgr8).
Here is what my custom DatePickerDialog looks like with the reflection logic:
public class MyReflectionDatePickerDialog extends DatePickerDialog {
private DatePicker mDatePicker;
#RequiresApi(api = Build.VERSION_CODES.N)
public MyReflectionDatePickerDialog(#NonNull Context context, #Nullable OnDateSetListener listener,
int year, int month, int dayOfMonth) {
super(context, listener, year, month, dayOfMonth);
fixSpinner(context, year, month, dayOfMonth);
}
// include gist here from:
// https://gist.github.com/lognaturel/232395ee1079ff9e4b1b8e7096c3afaf
}
You would instantiate this custom view as shown below:
DatePickerDialog dialog;
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
dialog = new MyDatePickerDialog(this, onDateSetListener, dob.getYear(),
dob.getMonth() - 1, dob.getDay());
} else {
dialog = new DatePickerDialog(this, onDateSetListener, dob.getYear(),
dob.getMonth() - 1, dob.getDay());
}
You can try this.
datePicker.setCalendarViewShown(false);
You Should try to raise the
targetSdkVersion to 24 because of the android 7.0
I am building an app that uses a DatePickerDialog to allow the user to select their birthdate. Here's the code that loads the dialog right now:
private void selectBirthdate() {
int year, month, day;
if (mBirthDate == null) {
year = DEF_YEAR;
month = DEF_MON;
day = DEF_DAY;
}
else {
year = mBirthDate.get(Calendar.YEAR);
month = mBirthDate.get(Calendar.MONTH);
day = mBirthDate.get(Calendar.DAY_OF_MONTH);
}
new DatePickerDialog(
getActivity(),
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mBirthDate = new GregorianCalendar();
mBirthDate.set(Calendar.YEAR, year);
mBirthDate.set(Calendar.MONTH, month);
mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (mTxtBirthDate != null) {
mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
}
}
},
year,
month,
day
).show();
}
And here's what the dialog looks like when I load it:
However, they want to be able to use the old-style spinner DatePicker, because in the new Calendar view, it's not always obvious to the user that they can change the year. So I have been studying up on the topic, and according to what I have read, it should be possible to use themes to force the DatePickerDialog into Spinner mode. So here's what I've done.
First, I added the following to my styles.xml:
(Sorry for the screenshot. Apparently SO's parser can't handle XML.)
Then, I update the DatePickerDialog constructor to use my new style:
new DatePickerDialog(
getActivity(),
R.style.MyDialogTheme,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mBirthDate = new GregorianCalendar();
mBirthDate.set(Calendar.YEAR, year);
mBirthDate.set(Calendar.MONTH, month);
mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (mTxtBirthDate != null) {
mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
}
}
},
year,
month,
day
).show();
Now, when I load the dialog it looks like this:
Clearly something has changed; the dialog has a darker theme. But it's not doing what I want. It's still displaying the calendar view instead of the spinner view. Any idea what I might have missed?
Here is what worked for me:
1) Create these two styles in your style.xml
<style name="MySpinnerDatePickerStyle" parent="android:Theme.Material.Dialog">
<item name="android:datePickerStyle">#style/MySpinnerDatePicker</item>
</style>
<style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
2) Set "MySpinnerDatePickerStyle" as your style in your new DatePickerDialog
DatePickerDialog datePickerDialog = new DatePickerDialog(myContext, R.style.MySpinnerDatePickerStyle, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Hope this works for you..
Note: I am still trying to figure out how to style the spinner by changing color and text color
Similarly as #Abuzaid said, inside your AppTheme style, add the android:datePickerStyle item as shown in this AppTheme example below:
<!-- Example of a Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<!-- !Important! Add this to force the use of the Spinner mode -->
<item name="android:datePickerStyle">#style/myDatePickerStyle</item>
</style>
Add this style to your styles.xml (inside the values-v21 folder, becouse this option is available only for api 21+)
<style name="myDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
For Android version < 21, the spinner mode is the default. For 21+ the calendar mode is the new default mode.
In this case you can omit to specify the dialog theme becouse it will be inferred by the AppTheme that we have. For example: (With Java 8 lambda)
final Calendar cal = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateSelectedListener = (view, year, monthOfYear, dayOfMonth) -> {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear+1);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
};
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp= new DatePickerDialog(context, dateSelectedListener, year, month, day);
Reference: https://developer.android.com/reference/android/app/DatePickerDialog
Hope it can help! :)
Using the styles mentioned in the other answer to get the spinner only works on stock (or close to stock) Android devices, such as Pixel, Motorola, OnePlus, etc.
I made the mistake of testing on these devices only, and to my surprise, Samsung users were still complaining about the other picker, where they couldn't find how to change the birthdate year without having to scroll month by month.
The only cross device solution is using a library, that way you are sure you get the same spinner picker regardless of the underlying OS. I my case, I used SpinnerDatePicker, which is a pretty straightforward replacement for the native picker.
Simple solution with Material Componente
<style name="SpinnerDatePickerDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog">
<item name="android:datePickerStyle">#style/DatePickerStyle</item>
<item name="colorControlNormal">#color/teal_200</item>
</style>
<style name="DatePickerStyle" parent="#android:style/Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
For some reason ThemeOverlay.MaterialComponents.Dialog.Alert is not working with darkmode
I am tying to change the divider color of the DatePicker Dialog.
I already checked all duplicate questions but i didn't get anything which is working.
I want to use orange color instead of this blue color of this divider.
Right now my datepicker looks like this and i also want to remove this background of whole date picker.
I used this code for datepicker dialog
final Calendar calendarDate = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendarDate.set(Calendar.YEAR, year);
calendarDate.set(Calendar.MONTH, monthOfYear);
calendarDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
dateSetListener, calendarDate.get(Calendar.YEAR), calendarDate
.get(Calendar.MONTH), calendarDate
.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setCalendarViewShown(false);
datePickerDialog.show();
Screeshot :
Use Custom Style to change the divider color/theme from your drawable image as below:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="YourTheme" parent="#android:style/Theme">
<item name="android:divider">#drawable/dialog_divider_color</item>
</style>
</resources>
and update the AndroidManifest.xml file with the android:theme="YourTheme" for the <activity/>
I am creating a DatePickerDialog that I want to apply a custom theme to. It's only applying part of the custom theme. I asked for no title and a windowBackground that's grey and the resulting entire dialog (except for the numbers and month) is orange. What am I doing wrong? Here is my custom theme:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="GPSTheme" parent="android:style/Theme.Dialog">
<item name="android:textColor">#000000</item>
<item name="android:windowTitleStyle">#style/Theme.Sherlock.Light</item>
<item name="android:background">#drawable/orange_gradient</item>
<item name="android:windowBackground">#drawable/cool_gradient</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
Here is where I am calling the dialog (from within a fragment):
btnStartDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (DatesValid(true, year, monthOfYear, dayOfMonth)) {
StartDate.set(Calendar.YEAR, year);
StartDate.set(Calendar.MONTH, monthOfYear);
StartDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SetTextDates();
} else {
Toast.makeText(
getActivity(),
"Start Date cannot be greater than End Date",
Toast.LENGTH_SHORT).show();
}
GetDistanceTraveledForTime();
}
};
DatePickerDialog d = new DatePickerDialog(getActivity(),
R.style.GPSTheme, mDateSetListener,
StartDate.get(Calendar.YEAR), StartDate
.get(Calendar.MONTH), StartDate
.get(Calendar.DAY_OF_MONTH));
//DatePicker dp = d.getDatePicker();
//LinearLayout l = (LinearLayout)dp.getChildAt(0);
//l.setBackgroundColor(R.drawable.orange_gradient);
d.setIcon(R.drawable.calendar_2_icon);
d.show();
}
});
I looked here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml for all the inherited attributes of Theme.Dialog.
Here is a screen shot:
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);
}