Skip to content

Instantly share code, notes, and snippets.

@togramago
Last active May 8, 2019 07:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save togramago/f7389959927366edc855 to your computer and use it in GitHub Desktop.
Save togramago/f7389959927366edc855 to your computer and use it in GitHub Desktop.
NumberPicker with transparent selection dividers via Reflection
package com.runup.myapplication2.app;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.NumberPicker;
import java.lang.reflect.Field;
/**
* For this: http://stackoverflow.com/questions/24233556/changing-numberpicker-divider-color
* Based on this: http://stackoverflow.com/a/20291416/2915480
*/
public class ExtendedNumberPicker extends NumberPicker {
public ExtendedNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
Class<?> numberPickerClass = null;
try {
numberPickerClass = Class.forName("android.widget.NumberPicker");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field selectionDivider = null;
try {
selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
selectionDivider.setAccessible(true);
selectionDivider.set(this, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
@DE-liquid
Copy link

Thank you a lot.

@karoly-vig
Copy link

Thanks.

@parthanjaria
Copy link

Can we change the width of the divider. Any sample code?

@mrmojtabaa
Copy link

mrmojtabaa commented May 8, 2019

@parthanjaria yes we can
just like above code , add another field selectionDividerHeight

Field selectionDividerHeight = null;
    try {
           selectionDividerHeight = numberPickerClass.getDeclaredField("mSelectionDividerHeight");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

And then

    try {
           selectionDividerHeight.setAccessible(true);
           selectionDividerHeight.set(this, 10); // 10 is width of the divider.

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment