Is there a way to change the color of the two horizontal lines on the Android NumberPicker
?
Asked
Active
Viewed 3,006 times
0
-
4Have you tried googling your question? – Magicmarkker Dec 07 '12 at 14:52
2 Answers
0
Perhaps this will get you started:
http://devmobapps.blogspot.com/2011/09/numberpicker-use-and-customization-of.html

NeilMonday
- 2,680
- 2
- 25
- 29
-
Whilst this may theoretically answer the question, we would like you to include the essential parts of the linked article in your answer, and provide the [link for reference](http://meta.stackexchange.com/q/8259). Failing to do that leaves the answer at risk from link rot. – Kev Feb 25 '13 at 19:58
0
Solution 1
If you only care for changing the color of the NumberPicker here is a very simple solution that uses the following.
private void setDividerColor(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 then call the function like this
setDividerColor(mNumberPicker, Color.GREEN);
Solution 2
Although the first solution is simple it is limited. I'd recommend using this repository which provides MaterialNumberPickers that allow for much greater customization. There are instructions on how to add Gradle dependencies in the ReadMe of the repo so I won't include them here but MaterialNumberPickers can then be added in xml as follows.
<com.github.stephenvinouze.materialnumberpickercore.MaterialNumberPicker
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mnpMaxValue="50"
app:mnpMinValue="1"
app:mnpEditable="false"
app:mnpFontname="Hand.ttf"
app:mnpSeparatorColor="@color/colorAccent"
app:mnpTextColor="@color/colorPrimary"
app:mnpTextSize="16sp"
app:mnpTextStyle="bold"
app:mnpValue="10"
app:mnpWrapped="false" />
Additionally, MaterialNumberPicker extends NumberPicker so making this change in xml won't require any backend changes in your .java files.

Kevin Barron
- 88
- 2
- 9