3

I have a number picker for setting a data limit in MB. right now, I have numberPicker, contains numeric values in sequence like [1,2,3,....., 2000 MB].

But I want a numberPicker that should contain numeric values like [100,200,300,...., 2000MB]. How can I do this?

maazza
  • 7,016
  • 15
  • 63
  • 96
Pravin Kumar
  • 51
  • 1
  • 1
  • 6
  • show some code to help more – Saif Jan 04 '16 at 06:17
  • I think you can find a solution here. http://stackoverflow.com/questions/12979643/change-the-step-size-of-a-numberpicker – Yaw Asare Jan 04 '16 at 06:22
  • You can use array to display your customized values. Check this link >> http://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html – mgcaguioa Jan 04 '16 at 06:28

3 Answers3

6

Displayed array values for your picker

int NUMBER_OF_VALUES = 20; //num of values in the picker
int PICKER_RANGE = 100;
...
String[] displayedValues  = new String[NUMBER_OF_VALUES];
//Populate the array
for(int i=0; i<NUMBER_OF_VALUES; i++)
    displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
/* OR: if the array is easy to be hard-coded, then just hard-code it:
   String[] displayedValues = {"100", "200", "300", .....}; */

Set arr in your picker :

numPicker.setMinValue(0); 
numPicker.setMaxValue(displayedValues.size()-1);
numPicker.setDisplayedValues(displayedValues);

get/set the value of the picker :

//To get the current value in the picker
choosenValue = displayedValues[numPicker.getValue()]; 
//To set a new value (let's say 150)
for( int i=0; i<displayedValues.length ; i++ )
    if( displayedValues[i].equals("300") )
         numPicker.setValue(i);
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Saif
  • 723
  • 6
  • 21
  • can we set Integer array instead of string array? – Pravin Kumar Jan 04 '16 at 07:42
  • means show me your array you have to put in picker? – Saif Jan 04 '16 at 08:41
  • see this link :http://developer.android.com/reference/android/widget/NumberPicker.html#setDisplayedValues%28java.lang.String%5B%5D%29 on void setDisplayedValues(String[] value) you are not set int[] – Saif Jan 04 '16 at 08:45
0

Try the code below

int start = 100;
        String[] numbers = new String[20];
        for(int i =0 ; i < 20 ; i++) {
            numbers[i] = start + "";
            start = start + 100;
        }
        NumberPicker numberPicker = (NumberPicker) findViewById(R.id.id_number_picker);
        numberPicker.setMaxValue(20);
        numberPicker.setMinValue(1);
        numberPicker.setDisplayedValues(numbers);
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
0
final String[] nums = new String[21];
        for(int i=0; i<nums.length; i++) {
            nums[i] = Integer.toString((i+1)*100);
        }

numberPicker.setMinValue(0);
numberPicker.setMinValue(19);
numberPicker.setDisplayedValues(nums);
Saurabh Agarwal
  • 139
  • 1
  • 2