How to change the style of a DatePicker in android?

124,404

Solution 1

Try this. It's the easiest & most efficient way

<style name="datepicker" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/primary</item>
</style>

Solution 2

call like this

 button5.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub

 DialogFragment dialogfragment = new DatePickerDialogTheme();

 dialogfragment.show(getFragmentManager(), "Theme");

 }
 });

public static class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener{

     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState){
     final Calendar calendar = Calendar.getInstance();
     int year = calendar.get(Calendar.YEAR);
     int month = calendar.get(Calendar.MONTH);
     int day = calendar.get(Calendar.DAY_OF_MONTH);

//for one

//for two 

 DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
 AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);

//for three 
 DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);

// for four

   DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
 AlertDialog.THEME_HOLO_DARK,this,year,month,day);

//for five

 DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

//for six

 DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
 AlertDialog.THEME_TRADITIONAL,this,year,month,day);


     return datepickerdialog;
     }

     public void onDateSet(DatePicker view, int year, int month, int day){

     TextView textview = (TextView)getActivity().findViewById(R.id.textView1);

     textview.setText(day + ":" + (month+1) + ":" + year);

     }
     }

follow this it will give you all type date picker style(copy from this)

http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/

enter image description here

enter image description here

enter image description here

Solution 3

To change DatePicker colors (calendar mode) at application level define below properties.

<style name="MyAppTheme" parent="Theme.AppCompat.Light">
    <item name="colorAccent">#ff6d00</item>
    <item name="colorControlActivated">#33691e</item>
    <item name="android:selectableItemBackgroundBorderless">@color/colorPrimaryDark</item>
    <item name="colorControlHighlight">#d50000</item>
</style>

See http://www.zoftino.com/android-datepicker-example for other DatePicker custom styles

zoftino DatePicker examples

Solution 4

Create a new style

<style name="my_dialog_theme" parent="ThemeOverlay.AppCompat.Dialog">
    <item name="colorAccent">@color/colorAccent</item>                   <!--header background-->
    <item name="android:windowBackground">@color/colorPrimary</item>     <!--calendar background-->
    <item name="android:colorControlActivated">@color/colorAccent</item> <!--selected day-->
    <item name="android:textColorPrimary">@color/colorPrimaryText</item> <!--days of the month-->
    <item name="android:textColorSecondary">@color/colorAccent</item>    <!--days of the week-->
</style>

Then initialize the dialog

Calendar mCalendar = new GregorianCalendar();
mCalendar.setTime(new Date());

new DatePickerDialog(mContext, R.style.my_dialog_theme, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        //do something with the date
    }
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)).show();

Result:

enter image description here

Solution 5

Calendar calendar = Calendar.getInstance();

DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.DatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Calendar newDate = Calendar.getInstance();
    newDate.set(year, monthOfYear, dayOfMonth);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String date = simpleDateFormat.format(newDate.getTime());
  }
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

datePickerDialog.show();

And use this style:

<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
  <item name="colorAccent">@color/colorPrimary</item>
</style>
Share:
124,404
silverFoxA
Author by

silverFoxA

Started as a graphics designer, became a programmer, advanced to a developer and now an entrepreneur. Co-Founder - Madgeek Check out my YouTube Channel for Development YouTube Channel

Updated on July 05, 2022

Comments

  • silverFoxA
    silverFoxA almost 2 years

    I want to change the default color of the date/time picker dialog in Android, so that it should match my app's theme. I searched for the solution on Google, but I couldn't find a solution.

    What I was doing was creating a new style:

      <style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker">
        <!---TODO-->
        <item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
    </style>
    

    Don't know what are the attributes available for date picker dialogue. It would be great if anyone could post a link on that

    and after adding the style I was calling it in my main style as

     <item name="android:datePickerStyle">@style/datepicker</item>
    

    Unfortunately, this didn't work for me at all.

  • kaya
    kaya over 7 years
    @WolfJee use like this; int theme; if (Build.VERSION.SDK_INT < 23) theme = AlertDialog.THEME_HOLO_DARK; else theme = android.R.style.Theme_Holo_Dialog; new DatePickerDialog(getActivity(), theme, date,......
  • Shirish Herwade
    Shirish Herwade over 6 years
    very good answer, if you don't want any change in styles and themes of application only for datePicker. But you have just messed up with example images' numbers, e.g .THEME_DEVICE_DEFAULT_LIGHT corresponds to second image I think. Also I'm getting now deprecated error for above code for API >23
  • Someone Somewhere
    Someone Somewhere about 6 years
    I hate the new "Material" style for Google's date picker because users have no fricken clue that the year can be changed by clicking in the top-left corner!! Horrible UX!
  • Someone Somewhere
    Someone Somewhere about 6 years
    @kaya your suggestion isn't working. Try it on Android 7.0 emulator
  • Someone Somewhere
    Someone Somewhere about 6 years
    @WolfJee did you find a solution?
  • silverFoxA
    silverFoxA about 6 years
    Isn't it the same code snippet as that of the accepted answer?
  • Nunchucks
    Nunchucks about 6 years
    Almost, but the name is different. Setting the name to 'datepicker' didn't work for me, but changing it to 'CalendarDatePickerDialog' made it work.
  • Kavin Raju S
    Kavin Raju S almost 4 years
    How to set the button style? I tried setting buttonBarNegativeButtonStyle buttonBarNeutralButtonStyle buttonBarPositiveButtonStyle buttonStyle but not reflecting.
  • Mahdi
    Mahdi over 3 years
    I have same issue and can not change button style
  • Mahdi
    Mahdi over 3 years
    How I can change button text colors?
  • Mahdi
    Mahdi over 3 years
    How I can change button style or button text color?
  • sejn
    sejn over 3 years
    Can I able to change the corner radius?
  • J A S K I E R
    J A S K I E R over 3 years
    Took no effect with Kotlin.
  • Daniyal Javaid
    Daniyal Javaid about 3 years
    Thanks. i was having invisible/white positive and negative buttons before. This fixed it.
  • Admin
    Admin about 3 years
    @Mahdi see Rohit's answer (that will change the button text color)
  • Admin
    Admin about 3 years
    @Mahdi see Rohit's answer (that will change the button text color)
  • user924
    user924 almost 3 years
    what if DatePicker is used in xml?
  • user924
    user924 almost 3 years
    what if DatePicker is used in xml?
  • Hitesh Dhamshaniya
    Hitesh Dhamshaniya about 2 years
    It works like a charm. Thank you for sharing...