10
votes

How is it possible to get a DatePickerDialog with Holo Light theme?

When creating a DatePickerDialog as follows:

 DatePickerDialog dpd = new DatePickerDialog(new ContextThemeWrapper(this,
                    android.R.style.Theme_Holo_Light_Dialog_NoActionBar), 
    new DateListener(v), mTime.year, mTime.month, mTime.monthDay);

or with theme android.R.style.Theme_Holo_Light or android.R.style.Theme_Holo_Light_Dialog, I get a date picker with a standard title and standard buttons. I tried to use a custom theme with a holo light parent too, but it didn't work either. It seems to work with theme android.R.style.Theme_Holo, but the result is a dark background (as expected), but I would like to have a light one.

The application's android.jar is of version 14, the application is running on a divice with android version 3.2.

I have seen an example here: http://as400samplecode.blogspot.com/2011/10/android-datepickerdialog.html, which shows a DatePickerDialog with the holo light theme, the way I would like to have it. I don't know why it doesn't work with my setup.

Thank you for help.

3
put android:theme="@android:style/Theme.Holo.Light" in your activity tag in manifest.xml - Padma Kumar
@PadmaKumar thank you for the comment. I will try what you suggested. But generally, I would like to use a theme with parent android:Theme.Holo.Light.DialogWhenLarge for the activity. - Julia
@PadmaKumar I put android:theme="@android:style/Theme.Holo.Light" in the activity tag in the manifest.xml and it didn't change anything (I think, because I use the 'ContextThemeWrapper'). - Julia
@Julia: did you ever find the solution to this? - Adrian Grigore
@Julia I'm not sure what's the problem here. I tested your code with android.R.style.Theme_Holo and with android.R.style.Theme_Holo_Light and both gave me the same dialog, with the same buttons. The only difference was the dark/light style. What seems to be the problem then? Perhaps you should update your question with screenshots? - rfgamaral

3 Answers

18
votes

The DatePickerDialog has a constructor which accepts a theme

DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

just update your code to include the style you want without the need for a ContextThemeWrapper

DatePickerDialog dpd = new DatePickerDialog(this,
                android.R.style.Theme_Holo_Light_Dialog_NoActionBar, 
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);

It's working for me that way.

3
votes

Read this Android DatePickerDialog example code which includes how to use different Themes in DatePickerDialog.

Here is the such one.

DatePickerDialog Constructor which support to define Theme.

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(), AlertDialog.THEME_HOLO_DARK, this, year, month, day); 
3
votes

For Material style this worked for me:

int datePickerThemeResId = 0;
if (android.os.Build.VERSION.SDK_INT >= 21) {
    datePickerThemeResId = android.R.style.Theme_Material_Light_Dialog;
}
new DatePickerDialog(
    context,
    datePickerThemeResId,
    (view, year, month, dayOfMonth) -> {},
    year,
    month,
    day
).show();