1

I wanted to customize number picker selection divider.

number picker selection divider

I created custom drawable to be used for number picker selection divider.

I created custom material style inheriting number picker material style.

<style name="MyNumberPickerTheme" parent="@android:style/Widget.Material.NumberPicker">
    <item name="colorControlNormal ">#2e7d32</item>
    <item name="android:background">#69f0ae</item>
    <item name="selectionDivider">@drawable/my_number_picker_divider</item>
</style>

But selectionDivider property is not recognized. How can I add my custom drawable to number picker material style to set custom selection divider?

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31
  • check this https://stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416. – Raghunandan Oct 10 '17 at 05:39
  • That is not useful. Its not color of the selection divider that I want to change. I created a drawable to be used as selection divider of number picker. But can't assign it to number picker as selectionDivider property is not recognized getting compile error, even android:selectionDivider same issue. – Arnav Rao Oct 10 '17 at 05:59
  • you can set a drawable i haven't tried. but the posted link has a answer which uses reflection and a library is also suggested. take a look at the answer modify it accordingly – Raghunandan Oct 10 '17 at 06:01

1 Answers1

1

It seems like there is no easy way to accomplish this. This StackOverflow solution is the only good enough solution in this case. For the sake of other readers, I'll copy/paste the answer here.

private void setDividerColor(NumberPicker picker, Drawable customDrawable) {

    java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
    for (java.lang.reflect.Field pf : pickerFields) {
        if (pf.getName().equals("mSelectionDivider")) {
            pf.setAccessible(true);
            try {
                pf.set(picker, customDrawable);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}

And now somewhere in your code, you can call setDividerColor(picker, drawableObject) to set the color of the divider.

capt.swag
  • 10,335
  • 2
  • 41
  • 41