2

I´m tying to change the divider color of the DatePicker Dialog.

I create the style:

<style name="dialog_custom" parent="@android:style/Widget.DatePicker">
        <item name="android:divider">@drawable/dialog_divider</item>
    </style>

And create the drawable like this

And the result is this

The divider no change color and the dialog take the content size..

2 Answers2

1

This is my solution to change divider colors in NumberPickers, TimePickers, DatePickers and the TimePickerDialog. For DatePickerDialog you can call DatePickerDialog.getDatePicker()

public class NumberPickerStylingUtils {

private static final Drawable PICKER_DIVIDER_DRAWABLE = //Place your drawable here

private NumberPickerStylingUtils() {}

public static void applyStyling(TimePickerDialog timePickerDialog) {
    try {
        Field field = TimePickerDialog.class.getDeclaredField("mTimePicker");
        field.setAccessible(true);
        applyStyling((TimePicker) field.get(timePickerDialog));
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(TimePicker timePicker) {
    try {
        Field fields[] = TimePicker.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().equals(NumberPicker.class)) {
                field.setAccessible(true);
                applyStyling((NumberPicker) field.get(timePicker));
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(DatePicker datePicker) {
    try {
        Field fields[] = DatePicker.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().equals(NumberPicker.class)) {
                field.setAccessible(true);
                applyStyling((NumberPicker) field.get(datePicker));
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void applyStyling(NumberPicker numberPicker) {
    try {
        Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
        field.setAccessible(true);
        field.set(numberPicker, PICKER_DIVIDER_DRAWABLE));
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

}

lilienberg
  • 1,252
  • 11
  • 18
-1

You can do this using theme. Check accepted answer on this question. i think it will helpful to you.

UPDATES

Expand res folder in your application and expand values folder. Then create themes.xml file on values folder. then replace all code in themes.xml file with below code.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="MYTheme" parent="@android:style/Theme">

       <item name="android:divider">@drawable/dialog_divider</item>

    </style>

</resources>

Then open your AndroidManifest.xml file. and find android:theme and replcae with android:theme="@style/MYTheme"

Community
  • 1
  • 1
Bishan
  • 15,211
  • 52
  • 164
  • 258
  • When I do this I get this error: error: Error: No resource found that matches the given name: attr 'android:_DEFAULT_BASE_COLOR_1'. – Alberto Bailac Moreno Feb 13 '13 at 08:32
  • Did you define `_DEFAULT_BASE_COLOR_1` in your resources ? – Bishan Feb 13 '13 at 08:46
  • Sorry but...What resources? How I can define it? – Alberto Bailac Moreno Feb 13 '13 at 09:04
  • I try generate all resources with this, http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html. But the default color of the dividers no change.. – Alberto Bailac Moreno Feb 13 '13 at 09:29
  • sorry but i get this error again..see the link https://dl.dropbox.com/u/30331820/ERROR.png – Alberto Bailac Moreno Feb 13 '13 at 11:09
  • updated my answer with `@drawable/dialog_divider` – Bishan Feb 13 '13 at 11:18
  • I set a style with android-ui-utils.googlecode.com like this: , with all the nine-patch drawables but this line blue not change. I create de "dialog_divider" like a nine-patch and put in the style but the line blue persist...any way to change the blue line divider in android style holo.light? – Alberto Bailac Moreno Feb 14 '13 at 08:37
  • Did you find any solution? This answer is accepted but doesn't provide any… – Marc Plano-Lesay Nov 07 '13 at 17:31
  • @Kernald In-detail analysis here: [Android: how to change the color of the datepicker divider?](http://stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider). – user3264740 Feb 14 '14 at 21:42