Change the color of selected date only in date picker android

允我心安 提交于 2019-12-02 18:22:58

问题


I am using date picker and I need to style it with different colors. All things are working fine except the selected date color. The thing is selected date color takes "android:textColorPrimaryInverse" as default which in my case is red , background of the dialog is white and the "android:colorControlHighlight" and "android:colorControlActivated" is also red.

This means the selected text color doesn't show.I need that to be shown as white but the issue is if I change"android:textColorPrimaryInverse" to white my title won't appear as the background is white.

here is my style file :

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/color_white</item>
    <item name="android:textColorPrimaryInverse">@color/red</item>
    <item name="android:textColorSecondaryInverse">@color/red</item>
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
    <item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
    <item name="android:colorControlHighlight">@color/red</item>
    <item name="android:colorControlActivated">@color/red</item>
    </style>

and this is how I show my dialog

 new DatePickerDialog(mActivity, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //DO SOMETHING
        }
    }, 2015, 02, 26).show();

回答1:


Use;

<item name="android:textColorPrimaryInverse">@color/white</item>
<item name="android:colorControlActivated">@color/red</item>



回答2:


Try below code

 <style name="CustomDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/accent</item>
        <item name="android:datePickerStyle">@style/CustomDatePickerStyle</item>
        <item name="android:colorAccent">@color/primDark</item>
    </style>

    <style name="CustomDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
        <item name="android:headerBackground">@color/prim</item>
        <item name="android:calendarTextColor">@color/primDark</item>
        <item name="android:dayOfWeekBackground">@color/primDark</item>
        <item name="android:yearListSelectorColor">@color/accent</item>
        <item name="android:datePickerMode">calendar</item>
    </style>



回答3:


What you need to deal with in your style is 'colorControlActivated' and 'selectableItemBackgroundBorderless' . Check the code snippet below:

    <item name="colorControlActivated">#@color/Green</item>
    <item name="android:selectableItemBackgroundBorderless">@color/LightPink</item>

Current date will be 'Green' and selected date will have a 'LightPink' boardering background




回答4:


<item name="android:colorControlActivated">@color/red</item>

This worked for me.



来源:https://stackoverflow.com/questions/43512895/change-the-color-of-selected-date-only-in-date-picker-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!