简体   繁体   中英

Change DatePicker header text color Android

I'm not using any library like MaterialDialog or something like that, I'm just using the default DatePicker of Android in a custom dialog. What I want, is to change the header background and text. The background (transparent) works but the text not. That's what i've done so far:

The DatePicker inside date_time_dialog_picker.xml layout

<DatePicker
            android:id="@+id/datePickerDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="true"
            android:datePickerMode="calendar"
            android:layout_gravity="center_horizontal"
            android:endYear="2100"
            android:maxDate="12/31/2100"
            android:minDate="01/01/2019"
            android:spinnersShown="true"
            style="@style/CalendarDatePickerDialog"
            android:startYear="2019" />

The style

<style name="CalendarDatePickerDialog" parent="Theme.AppCompat.Light">
        <item name="android:headerBackground">@android:color/transparent</item>
        <item name="android:colorControlNormal">@color/colorPrimary</item>
        <item name="android:colorControlHighlight">@color/colorPrimary</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:textColorPrimaryInverse">@color/colorPrimaryDark</item> <!-- header date, month color && calendar text highlight color -->
        <item name="android:textColorSecondaryInverse">@color/colorPrimaryDark</item> <!-- header year color -->
    </style>

and then the java code

private void showDatePicker() {
        LayoutInflater inflater = getLayoutInflater();
        final View customView = inflater.inflate(R.layout.date_time_dialog_picker, null);
 Calendar dateTimeCalendar = Calendar.getInstance();
dateTimeCalendar.setTimeZone(TimeZone.getDefault());
year = dateTimeCalendar.get(Calendar.YEAR);
        month = dateTimeCalendar.get(Calendar.MONTH);
        day = dateTimeCalendar.get(Calendar.DAY_OF_MONTH);

 final DatePicker dpStartDate = customView.findViewById(R.id.datePickerDialog);
 dpStartDate.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                MyActivity.this.year = year;
                MyActivity.this.month = month;
                MyActivity.this.day = day;
            }
        });

AlertDialog.Builder builder = new AlertDialog.Builder(AddPermessoActivity.this);
        builder.setView(customView); // Set the view of the dialog to your custom layout
        builder.setTitle("");
        builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "date: " + MyActivity.this.year + " " + MyActivity.this.month + " " + MyActivity.this.day);
                dialog.dismiss();
            }});

        // Create and show the dialog
        builder.create().show();
}

So, it seems that only this property works

 <item name="android:headerBackground">@android:color/transparent</item>

Thanks

Add

<item name="android:textColorPrimaryInverse">@color/colorPrimary</item>

under App base theme

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

     <!-- header date, month color && calendar text highlight color -->
    <item name="android:textColorPrimaryInverse">@color/colorPrimary</item> 
 </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM