Increase Number Picker Wheel Item Count - Default set to 3 items in the Wheel

2.5k Views Asked by At

Currently by default the number picker only displays 3 values/items/elements at a time. I thought this will be easily changed but I was wrong. I cannot figure out how to change this. I looked around and could not find a way to change it. Maybe there is another way that I am not aware of.

I saw this post but its 5 years ago so I assume its Java and it mentions the same issue that I have

NumberPicker Display more than 3 values. Modify Android Internal Class

The post did not provide an answer but gave me something to look into. This is the comment from the other post

"Make the SELECTOR_WHEEL_ITEM_COUNT = 5; or something else and change the height of NumberPicker = "300dp" (Depending upon ITEM_COUNT) It is working as expected. I tried just now."

I typed the code into MainActivity but does not work. I then found NumberPicker.java class inside of Libraries > android > Widget > NumberPicker.java and found this

@Widget
public class NumberPicker extends LinearLayout {
/**
 * The number of items show in the selector wheel.
 */
@UnsupportedAppUsage
private static final int SELECTOR_WHEEL_ITEM_COUNT = 3;

I changed the 3 to a 5 but nothing happened... the Wheel still display 3 items. I assume this code is not being used. How can I change the item count in a wheel form 3 to 5 in Kotlin? Is there an easy way to do this? This has been very complicated for something that seems easy to do

Thanks!

3

There are 3 best solutions below

0
On

I was told that you cannot do this with the default number picker that android studio has. I was referred to this which has more customization for number picker

https://android-arsenal.com/details/1/7169

But I do not know how to import it to my project :(

I might submit another post if I could not figure out how to import this to my build but I will keep this up so that who ever has this issue will know that you cannot change this with the default number picker in android studio

1
On

NumberPicker.java now supports to set numbers of displayed items with setWheelItemCount(int count) function.

0
On

Yes, this is seems to be unsupported in later versions of NumberPicker. So you have to take earlier version. For example, from here: https://github.com/KnIfER/ODPlayer/blob/master/filepicker/src/main/java/com/knziha/filepicker/view/NumberKicker.java

Thus, you will be able to set any value to SELECTOR_WHEEL_ITEM_COUNT. And then use it in your xml instead of base NumberPicker, something like:

<com.your.package.NumberKicker
....