Android numberpicker text size
The android library that provides a simple and customizable NumberPicker. It’s based on android.widget.NumberPicker.
- Customizable fonts(color, size, strikethrough, underline, typeface)
- Customizable dividers(color, distance, length, thickness, type)
- Horizontal and Vertical mode are both supported
- Ascending and Descending order are both supported
- Also supports negative values and multiple lines
attribute name | attribute description | default |
---|---|---|
np_width | The width of this widget. | |
np_height | The height of this widget. | |
np_accessibilityDescriptionEnabled | Flag whether the accessibility description enabled. | enabled |
np_dividerColor | The color of the selection divider. | |
np_dividerDistance | The distance between the two selection dividers. | |
np_dividerLength | The length of the selection divider. | |
np_dividerThickness | The thickness of the selection divider. | |
np_dividerType | The type of the selection divider. | side_lines |
np_fadingEdgeEnabled | Flag whether the fading edge should enabled. | |
np_fadingEdgeStrength | The strength of fading edge while drawing the selector. | |
np_formatter | The formatter of the numbers. | |
np_hideWheelUntilFocused | Flag whether the selector wheel should hidden until the picker has focus. | |
np_itemSpacing | Amount of space between items. | |
np_lineSpacingMultiplier | The line spacing multiplier for the multiple lines. | |
np_max | The max value of this widget. | |
np_maxFlingVelocityCoefficient | The coefficient to adjust (divide) the max fling velocity. | |
np_min | The min value of this widget. | |
np_order | The order of this widget. | ascending |
np_orientation | The orientation of this widget. | vertical |
np_scrollerEnabled | Flag whether the scroller should enabled. | |
np_selectedTextAlign | The text align of the selected number. | center |
np_selectedTextColor | The text color of the selected number. | |
np_selectedTextSize | The text size of the selected number. | |
np_selectedTextStrikeThru | Flag whether the selected text should strikethroughed. | |
np_selectedTextUnderline | Flag whether the selected text should underlined. | |
np_selectedTypeface | The typeface of the selected numbers. | |
np_textAlign | The text align of the numbers. | center |
np_textColor | The text color of the numbers. | |
np_textSize | The text size of the numbers. | |
np_textStrikeThru | Flag whether the text should strikethroughed. | |
np_textUnderline | Flag whether the text should underlined. | |
np_typeface | The typeface of the numbers. | |
np_value | The current value of this widget. | |
np_wheelItemCount | The number of items show in the selector wheel. | |
np_wrapSelectorWheel | Flag whether the selector should wrap around. |
Add the dependency in your build.gradle
Thank you to all our backers! 🙏
The source code is licensed under the MIT license.
About
🎰 The android library that provides a simple and customizable NumberPicker.
Источник
NumberPicker
Recently in my day job I needed to use a NumberPicker which is a control I have not had reason to use for quite a long time. The experience was quite a frustrating one and in this post we’ll look at some of the issues with it, and some work-arounds.
A NumberPicker is a control for selecting a number from a limited range. It is used within TimePicker but is also a standalone widget in its own right. There are various forms of rendering it which depend on the theme of your app. I’ll focus on the Material-themed variant (shown on the left) which will be used if your app’s theme derives from R.style.Theme_Material (which the Material Components themes do). It is a vertical list of the numbers in the defined range and the user can either swipe on the list to scroll it to the desired number, or they can tap on the areas above and below the dividers to increment or decrement the value. Also they can long press the same ares to perform repeated increment or decrement which will continue until the long press ceases.
NumberPicker first appeared in Android 3.0 (Honeycomb – API 11). The seasoned Android developers reading this will be groaning at the memory of Honeycomb because it was a rushed release to make Android works on larger devices – specifically tablets because Apple had launched the iPad, and Google wanted to be able to compete with that. NumberPicker is actually a sad reflection of the rushed nature of Honeycomb which has never seen much love since.
NumberPicker actually works quite well, albeit with some caveats which we’ll come to shortly, but the real problems arise when we come to try and style it.
The first indication that there are issues with the implementation of NumberPicker is how we define the range of numbers that it will display. This is easy enough using the setMinValue() and setMaxValue() methods, but these are not exposed as XML attributes so we cannot specify them within our XML layouts – we have to set them programmatically. Compare this to a control such as ProgressBar where we can specify the progress range wither programmatically or in the layout XML.
We can also override the actual strings displayed using the setDisplayedValues() method with an array of String s. This enables us to use both a non-contiguous range (such as 5, 10, 15, 20, …) or even text values (such as “Jan”, ‘Feb”, “Mar”, …). This allows for some nice flexibility in how we present things to the user but, once again, we can only do this programmatically. It would be really useful if we were able to specify a string-array resource in XML, but this is not supported.
Things become even trickier when it comes to styling and themeing a NumberPicker . The style that gets applied by default through the Material theme is defined in @style/Widget.Material.NumberPicker and this has a parent of @style/Widget.NumberPicker . This default style is applied through the theme attribute android:numberPickerStyle , so we should be able to create our own style and apply it by overriding android:numberPickerStyle in our theme.
Unfortunately this is hampered by the limited number of styleable attributes that are exposed for NumberPicker . If we take a look at these in the AOSP sources we can see that there are 11 attributes specific to NumberPicker but some of these are private (the ones with the internal prefix). Leaving 6 attributes that we can actually override, which allow some limited control of the divider appearance and positioning, plus the background colour and the drawable used to show the pressed state of the increment and decrement virtual buttons. This is pretty limited.
Источник
NumberPicker
Компонент NumberPicker находился в разделе Advanced старых версий студии. Компонент позволяет выбрать нужное число из заданного диапазона. Принцип работы похож на револьверный барабан — можно прокручивать числа в одну или другую сторону. Когда будет достигнут заданный предел, то числа продолжат изменяться в заданном диапазоне.
Учитывая, что компонент убрали из студии, можно предположить, что он оказался не очень востребованным.
Разместим компонент на экране.
Несмотря на то, что есть атрибут orientation, толку от него немного. Работает только вертикальный режим.
Добавляем немного кода:
Мы определили максимальное и минимальные числа для нашего вида. Удивительно, но через XML нельзя установить эти значения. Запустив проект, вы можете теперь выбрать нужное число через стрелочки. Недавно проверял на другом телефоне (API 19) — стрелочек уже не было.
Метод setWrapSelectorWheel() с параметром false может отключить бесконечную прокрутку:
Пример на Kotlin
Добавим на экран текстовую метку, чтобы отслеживать текущее выбранное значение.
Выбрать кота (Kotlin)
Мы можем заменить числа на свои значения через метод setDisplayedValues() (Java), например, на имена котов. В этом случае компонент приобретает какой-то смысл.
Про остальные методы почитайте в документации.
Источник
Android numberpicker text size
Copy raw contents
The android library that provides a simple and customizable NumberPicker. It’s based on android.widget.NumberPicker.
- Customizable fonts(color, size, strikethrough, underline, typeface)
- Customizable dividers(color, distance, length, thickness, type)
- Horizontal and Vertical mode are both supported
- Ascending and Descending order are both supported
- Also supports negative values and multiple lines
attribute name | attribute description | default |
---|---|---|
np_width | The width of this widget. | |
np_height | The height of this widget. | |
np_accessibilityDescriptionEnabled | Flag whether the accessibility description enabled. | enabled |
np_dividerColor | The color of the selection divider. | |
np_dividerDistance | The distance between the two selection dividers. | |
np_dividerLength | The length of the selection divider. | |
np_dividerThickness | The thickness of the selection divider. | |
np_dividerType | The type of the selection divider. | side_lines |
np_fadingEdgeEnabled | Flag whether the fading edge should enabled. | |
np_fadingEdgeStrength | The strength of fading edge while drawing the selector. | |
np_formatter | The formatter of the numbers. | |
np_hideWheelUntilFocused | Flag whether the selector wheel should hidden until the picker has focus. | |
np_itemSpacing | Amount of space between items. | |
np_lineSpacingMultiplier | The line spacing multiplier for the multiple lines. | |
np_max | The max value of this widget. | |
np_maxFlingVelocityCoefficient | The coefficient to adjust (divide) the max fling velocity. | |
np_min | The min value of this widget. | |
np_order | The order of this widget. | ascending |
np_orientation | The orientation of this widget. | vertical |
np_scrollerEnabled | Flag whether the scroller should enabled. | |
np_selectedTextAlign | The text align of the selected number. | center |
np_selectedTextColor | The text color of the selected number. | |
np_selectedTextSize | The text size of the selected number. | |
np_selectedTextStrikeThru | Flag whether the selected text should strikethroughed. | |
np_selectedTextUnderline | Flag whether the selected text should underlined. | |
np_selectedTypeface | The typeface of the selected numbers. | |
np_textAlign | The text align of the numbers. | center |
np_textColor | The text color of the numbers. | |
np_textSize | The text size of the numbers. | |
np_textStrikeThru | Flag whether the text should strikethroughed. | |
np_textUnderline | Flag whether the text should underlined. | |
np_typeface | The typeface of the numbers. | |
np_value | The current value of this widget. | |
np_wheelItemCount | The number of items show in the selector wheel. | |
np_wrapSelectorWheel | Flag whether the selector should wrap around. |
Add the dependency in your build.gradle
Thank you to all our backers! 🙏
The source code is licensed under the MIT license.
Источник