- Android NumberPickers
- What are NumberPickers?
- Creating a NumberPicker
- Setting Custom Values
- Retrieving NumberPicker Values
- Android numberpicker set value
- About
- Tutorialwing
- Output
- Getting Started
- 1. Creating New Project
- 2. Modify values folder
- 3. Modify Layout Folder
- 4.1 Create Android NumberPicker Programmatically / Dynamically And Set Integer Values
- 4.2 Create Android NumberPicker Programmatically / Dynamically And Set Array of String Values
- AndroidManifest.xml file
- Tutorialwing
- Output
- Getting Started
- Attributes of Android NumberPicker Widget
- Example of Android NumberPicker Widget
- 1. Creating New Project
- 2. Modify Values folder
- 3. Use NumberPicker Widget in xml file
- 4. Show Only Integer Values in NumberPicker
- 5. Show Array Of String Values in NumberPicker
- AndroidManifest.xml
Android NumberPickers
Oct 26, 2018 · 3 min read
In this tutorial you will learn how to create NumberPicker widgets in Android Studio with custom values and how to retrieve NumberPicker values.
What are NumberPickers?
NumberPickers are a widget avalible in Android Studio. They are a set of values selectable by either a horizontal or vertical scrolling wheel.
Creating a NumberPicker
To create a NumberPicker, go to the XML file of your main activity. Switch from the Design tab to the Text tab and type
When y o u switch back to the Design tab, the NumberPicker should be visible. The NumberPicker in the XML will look different from the NumberPicker in the app. Under ID, name the NumberPicker with the what_where_why naming scheme. In this case it will be numberpicker_main_picker. Don’t forget to constrain your NumberPicker! For this example the picker is centered horizontally and vertically.
In your MainActivity, create a NumberPicker variable and wire it to the NumberPicker you created in your XML.
Setting Custom Values
To set custom values on your NumberPicker, first set a minimum and maximum value. In this example we will have 5 values, so we will set the range to:
A NumberPicker takes an array of Strings as its values. First create an instance variable of a string array, then initialize the array with your desired values.
Next, set the NumberPicker values to these Strings with .setDisplayedValues()
When the program is run, it should look like this:
*note: the best way to format this code is to handle it under an initializePicker(); method, then run the method in onCreate. However, for the sake of simplicity in this tutorial, I will not be doing this.*
Retrieving NumberPicker Values
To get the value of your NumberPicker every time it is changed, create an onValueChangeListener in onCreate. In the overriden onValueChange method, create an int variable to store the picker value. Next, make a Log.d to log what the picker value is.
With the code we currently have, the NumberPicker value will be an int. To retrieve the String value of the picker we must access the String array storing our picker values. Rather than just logging the value of picker1, we must log pickerVals[valuePicker1].
Источник
Android numberpicker set value
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.
Источник
Tutorialwing
Hello Readers! In this post, we are going to learn how to create and use android numberPicker programmatically in any android application. We will also learn to add numberPicker in linearLayout programmatically in any application.
Output
Tutorialwing Android Dynamic NumberPicker Integer Output
Tutorialwing Android Dynamic NumberPicker Integer Output
Tutorialwing Android Dynamic NumberPicker String Output
Tutorialwing Android Dynamic NumberPicker String Output
Getting Started
At first, we will create an android application. Then, we will use numberPicker widget in the application.
1. Creating New Project
Follow the steps below to create a new project. Please ignore the steps if you have already created a new project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as DynamicNumberPicker. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use android numberPicker programmatically.
2. Modify values folder
Open res/values/strings.xml file and add below code into it.
3. Modify Layout Folder
Open res/layout/activity_main.xml file. Then, add below code into it.
In activity_main.xml file, we have defined linearLayout, with id rootContainer, that will act as container for the numberPicker created dynamically in the application.
We can set two types of data in the numberPicker. They are –
a. Provide Only Integer values in the numberPicker.
b. Provide array of string values in the numberPicker.
4.1 Create Android NumberPicker Programmatically / Dynamically And Set Integer Values
Here, We will create numberPicker dynamically and set integer values in it. So, Open app/src/main/java/com.tutorialwing.dynamicnumberpicker/MainActivity.java file and add below code into it.
In MainActivity.java file, we have created numberPicker dynamically as set layout params etc. in it. Then, we have used setMinValue() method to set minimum value in it. After that, we have used setMaxValue() method to set maximum value in it. Then, we have set a listener to show toast message whenever there is change in value selection in numberPicker. At last, we have added numberPicker in linearLayout having id rootContainer.
4.2 Create Android NumberPicker Programmatically / Dynamically And Set Array of String Values
Here, We will create numberPicker dynamically and set array of string values in it. So, Open app/src/main/java/com.tutorialwing.dynamicnumberpicker/MainActivity.java file and add below code into it.
Here, we have created an array of string values (with name values). Then, we have set minimum and maximum range of values using setMinValue and setMaxValue method. After that, we have used setDisplayedValues() method to set array of string values in numberPicker.
Since AndroidManifest.xml file is very important in any android project. We are also going to see the content inside this file.
AndroidManifest.xml file
Code inside src/main/AndroidManifest.xml file would look like below –
When we run the application, we will get output as shown above.
That’s the end of tutorial on Creating Android NumberPicker Programmatically.
Источник
Tutorialwing
Hello Readers! In this post, we are going to learn about how to use android numberPicker widget in any android application. We will also go through different attributes of numberPicker widget that can be used to customise it.
Output
Tutorialwing Android NumberPicker Integer Output
Tutorialwing Android NumberPicker Integer Output
Tutorialwing Android NumberPicker String output
Tutorialwing Android NumberPicker String output
Getting Started
NumberPicker widget can be defined as below –
NumberPicker is a widget that allows user to select a number from predefined range.
Attributes of Android NumberPicker Widget
Some of the popular attributes of android numberPicker inherited from linearLayout are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:divider | Drawable to use as a vertical divider between buttons |
2 | android:gravity | Specifies how an object should be placed inside LinearLayout i.e. specifies position, CENTER, VERTICAL_CENTER, HORIZONTAL_CENTER etc. relative to the boundaries of the linearLayout |
3 | android:orientation | Specifies whether linearLayout should be column or row. i.e. views inside linearLayout will be aligned as horizontally or vertically. |
4 | android:weightSum | Specifies maximum weight sum |
Some of the popular attribute of android numberPicker inherited from viewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animationCache | Defines whether layout animation should create a drawing cache for their children |
2 | android:clipChildren | Defines whether a child is limited to draw inside of its bounds or not |
3 | android:clipToPadding | Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero |
4 | android:layoutMode | Defines layout mode of this viewGroup |
Some of the popular attribute of android numberPicker inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Sets alpha of the view |
2 | android:background | Sets background of the view |
3 | android:focusable | Sets whether view can take focus or not |
4 | android:id | Sets unique identifier of the view |
5 | android:padding | Sets padding of view |
6 | android:visibility | Sets whether this view is visible or not |
Example of Android NumberPicker Widget
At first, we will create android application. Then, we will use numberPicker widget in this application.
1. Creating New Project
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as NumberPicker. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use numberPicker widget in the application.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
3. Use NumberPicker Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
In activity_main.xml file, we have defined android numberPicker widget. Now, we will access this widget in java file and perform some operations on it.
We can provide two type of data to the numberPicker. They are –
a. Only Integer values.
b. Array of String values.
So, we will see how we can provide each type of data to the numberPicker. At first, we will see how to provide only integer values to the numberPicker.
4. Show Only Integer Values in NumberPicker
Open src/main/java/com.tutorialwing.numberpicker/MainActivity.java file. Then, add below code into it.
Here, we are only providing integer values to the numberPicker. setMinValue() method sets the minimum value to the numberPicker. setMaxValue() method sets maximum value the numberPicker. setWrapSelectorWheel() sets whether first number should be shown once we reach at end. Finally, we have set a listener to show toast message that displays the new value and old value.
5. Show Array Of String Values in NumberPicker
Open src/main/java/com.tutorialwing.numberpicker/MainActivity.java file. Then, add below code into it.
Here, we are providing array of string values to the numberPicker. values contains arrays of string values. setDisplayedValues() method sets the values to be displayed in the numberPicker. Remaining methods are same as we discussed above.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
When we run the program, we will get output as shown above.
That’s end of tutorial on Android NumberPicker widget.
Источник