19

How can I remove divider lines in number picker, I tried setShowDivider to none(seems none doesn't exist) through xml and code noting worked

picker.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

XML:

android:showDividers="none"

enter image description here

blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • I had used a similar solution to the first one by @AdityaVyas-Lakhan by creating a custom theme. Use android:color/transparent to access transparent color, if that helps. Alternatively, you can refer this https://stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416 and refer the approach used for Datepicker. – Bhavita Lalwani Jun 13 '17 at 12:02

4 Answers4

27

Set Theme for Numberpicker

<NumberPicker
   ...
   android:theme="@style/DefaultNumberPickerTheme" />

style.xml

<style name="DefaultNumberPickerTheme" parent="AppTheme">
        <item name="colorControlNormal">@color/transparent</item>
</style>

OR

private void changeDividerColor(NumberPicker picker, int color) {

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

And Set it as

changeDividerColor(yournumberpicker, Color.parseColor("#00ffffff"));
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
  • 2
    why its so difficult to remove/make invisible merely 2 lines in number picker? any other option instead number picker? – blackHawk Jun 13 '17 at 15:25
7

Add One of those line in your NumberPicker

XML : android:selectionDividerHeight="0dp"

OR

JAVA: picker.setSelectionDividerHeight(0)

Saiful Islam
  • 1,151
  • 13
  • 22
5

There is a simple solution. Calling picker.setSelectionDividerHeight(0) could do the trick.

Ilnar Ramazanov
  • 121
  • 1
  • 3
3

this code would be better

private void changeDividerColor(NumberPicker picker, int color) {
        try {
            Field mField = NumberPicker.class.getDeclaredField("mSelectionDivider");
            mField.setAccessible(true);
            ColorDrawable colorDrawable = new ColorDrawable(color);
            mField.set(picker, colorDrawable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
coffee
  • 81
  • 3
  • 3
    Isn't this a greylist reflection call since Android 9? Is that still working (soon)? See: https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces – Tobias Reich Aug 07 '19 at 10:30