Last active
May 8, 2019 07:35
-
-
Save togramago/f7389959927366edc855 to your computer and use it in GitHub Desktop.
NumberPicker with transparent selection dividers via Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Thanks.
Can we change the width of the divider. Any sample code?
@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
Thank you a lot.