How to change the style of a DatePicker in android?

后端 未结 7 1480
孤街浪徒
孤街浪徒 2020-11-30 19:29

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

相关标签:
7条回答
  • 2020-11-30 19:34

    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:

    0 讨论(0)
  • 2020-11-30 19:36
    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>
    
    0 讨论(0)
  • 2020-11-30 19:38

    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

    0 讨论(0)
  • 2020-11-30 19:39

    As AlertDialog.THEME attributes are deprecated, while creating DatePickerDialog you should pass one of these parameters for int themeResId

    • android.R.style.Theme_DeviceDefault_Dialog_Alert
    • android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
    • android.R.style.Theme_Material_Light_Dialog_Alert
    • android.R.style.Theme_Material_Dialog_Alert
    0 讨论(0)
  • 2020-11-30 19:52

    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>
    
    0 讨论(0)
  • 2020-11-30 19:55

    (I'm using React Native; targetSdkVersion 22). I'm trying to change the look of a calendar date picker dialog. The accepted answer didn't work for me, but this did. Hope this snippet helps some of you.

    <style name="CalendarDatePickerDialog"  parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#6bf442</item>
        <item name="android:textColorPrimary">#6bf442</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题