KevinB
KevinB

Reputation: 2484

Android - Change lines color from DatePickerDialog

I use Theme.Holo.Light.Dialog.NoActionBar.MinWidth for my DatePickerDialog, but I would like to change the blue color, in order to have it in red.

I changed the color of the text successfully but I didn't find how to change the color of the lines.

enter image description here

I tried a lot of things, this is my code currently:

<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog.NoActionBar.MinWidth">
        <item name="colorAccent">@color/smoothRed</item>
        <item name="colorPrimary">@color/smoothRed</item>
        <item name="android:textColor">@color/smoothRed</item>
        <item name="textColor">@color/smoothRed</item>
        <item name="android:background">@null</item>
        <item name="android:textColorPrimary">@color/colorPrimary</item>
    </style>

Upvotes: 2

Views: 902

Answers (1)

You can try this solution. works for me.

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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);


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

    colorizeDatePicker(datepickerdialog.getDatePicker());

    datepickerdialog.show();


}


    public static void colorizeDatePicker(DatePicker datePicker) {
        Resources system = Resources.getSystem();
        int dayId = system.getIdentifier("day", "id", "android");
        int monthId = system.getIdentifier("month", "id", "android");
        int yearId = system.getIdentifier("year", "id", "android");

        NumberPicker dayPicker = (NumberPicker) datePicker.findViewById(dayId);
        NumberPicker monthPicker = (NumberPicker) datePicker.findViewById(monthId);
        NumberPicker yearPicker = (NumberPicker) datePicker.findViewById(yearId);

        setDividerColor(dayPicker);
        setDividerColor(monthPicker);
        setDividerColor(yearPicker);
    }

    private static void setDividerColor(NumberPicker picker) {
        if (picker == null)
            return;

        final int count = picker.getChildCount();
        for (int i = 0; i < count; i++) {
            try {
                Field dividerField = picker.getClass().getDeclaredField("mSelectionDivider");
                dividerField.setAccessible(true);
                ColorDrawable colorDrawable = new ColorDrawable(picker.getResources().getColor(R.color.colorAccent));
                dividerField.set(picker, colorDrawable);
                picker.invalidate();
            } catch (Exception e) {
                Log.w("setDividerColor", e);
            }
        }

}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

}
}

Upvotes: 3

Related Questions