datepicker with different style than activity

29,203

Solution 1

I know this is little late but I walked into this topic while I was searching something else about DatePickers.

Add this to the custom theme you are using:

<item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>

This will override the parent theme you are using and result as a different looking DatePicker.

You will need to create the dialog with this custom theme.

Example of my theme:

<style name="CustomTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionButtonStyle">@style/MyActionBarButtonStyle</item>
    <item name="android:actionMenuTextColor">#ffffff</item>
    <item name="android:windowBackground">@drawable/bg</item>
    <item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>
</style>

Solution 2

Try like this:

style="@android:style/Widget.DatePicker"

Solution 3

DatePickerDialog dateDialog = new DatePickerDialog(getActivity(),
                                                   android.R.style.Theme_Holo_Light_Dialog,
                                                   ondateSet, year, month, day);
dateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

This is for showing white color light background Date picker Fragment.

Share:
29,203
mehdok
Author by

mehdok

Updated on July 23, 2022

Comments

  • mehdok
    mehdok almost 2 years

    I have activities with custom style, I want my activity with a custom title bar so I created below style:

    <style name="costum_titlebar">
        <item name="android:background">@color/titlebar_background</item>
    </style>
    
    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">@dimen/titlebar_size</item>
        <item name="android:windowTitleBackgroundStyle">@style/costum_titlebar</item>
    </style>
    

    In manifest I use:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/CustomTheme" >
    

    In the activity I use:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.setting_activity_titlebar);
    

    So far it's good and works fine, I have activity with my custom title bar.
    Now I want to use datePickerDialog. The problem is that dialog looks old:

    enter image description here

    But I want it to look like this:

    enter image description here

    I also have this style:

    <style name="AppBaseTheme" parent="android:Theme.Light">
    

    So I used it to create DatePickerDialog like this:

    new DatePickerDialog(getActivity(),R.style.AppBaseTheme, this, year, month, day);
    

    But the theme won't apply.

    Just for a test, I create a second app with default style and just one button to open date picker, so the datepicker Looks like what I want. Both apps have android:minSdkVersion="8"

    I think the problem is with custom titlebar, so how I can get my custom titlebar and datepicker with desired style?

    EDIT:

    I Changed android:minSdkVersion="11"
    with
    <style name="datePickerTheme" parent="@android:style/Widget.Holo.DatePicker"></style>
    and
    new DatePickerDialog(getActivity(),R.style.datePickerTheme, this, year, month, day);

    But the Result still is the same.

  • hBrent
    hBrent over 9 years
    For those less familiar with themes, if the activity in which you want to use the datepicker with the desired style doesn't have a theme, you'll put a custom theme in styles.xml, using code in the above format, and in the entry for that activity in the manifest, you'll add the line android:theme="@style/<Your.Theme>", replacing Your.Theme with whatever you've called the theme in styles.xml, of course.