Using NumberPicker Widget with Strings

Android

Android Problem Overview


Is there a way to use the Android NumberPicker widget for choosing strings instead of integers?

Android Solutions


Solution 1 - Android

NumberPicker picker = new NumberPicker(this);
picker.setMinValue(0);
picker.setMaxValue(2);
picker.setDisplayedValues( new String[] { "Belgium", "France", "United Kingdom" } );

Solution 2 - Android

NumberPicker numberPicker = new NumberPicker(this);
String[] arrayString= new String[]{"hakuna","matata","timon","and","pumba"};
numberPicker.setMinValue(0);
numberPicker.setMaxValue(arrayString.length-1);

numberPicker.setFormatter(new NumberPicker.Formatter() {
  @Override
  public String format(int value) {
	// TODO Auto-generated method stub
	return arrayString[value];
  }
});

hope that helps!

Solution 3 - Android

String value = String.valueOf(picker.getValue());

You could mean something else, but it isn't very clear what you mean.

UPDATED:

You can fake it by setting your own formatter:

CountryFormatter implements Formatter {
    public String toString(int value) {
        switch(value) {
            case 0:
                return "England";
            case 1:
                return "France";
        }
        return "Unknown";
    }
}

picker.setFormatter(new CountryFormatter());

getValue() will still return an int, so you probably want to map the names of countries to their ids.

ADDED: The implementation of NumberPicker has:

public void setRange(int start, int end, String[] displayedValues)

Which seems to do a better job of what you want then the above formatter.. although it isn't mentioned in the documentation so probably isn't part of the public api

Solution 4 - Android

try this solution, may help you

	NumberPicker pickers = new NumberPicker(this);
    String[] arrayPicker= new String[]{"abc","def","ghi","jkl","mno"};

    //set min value zero
	pickers.setMinValue(0);
	//set max value from length array string reduced 1
    pickers.setMaxValue(arrayPicker.length - 1);
	//implement array string to number picker
    pickers.setDisplayedValues(arrayPicker);
	//disable soft keyboard
    pickers.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
	//set wrap true or false, try it you will know the difference
	pickers.setWrapSelectorWheel(false);
	
	//create button to test selected text string
	btnPicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //get position (int) from number picker
            int pos = pickers.getValue();
			//get string from number picker and position
            String selecPicker = arrayPicker[pos];  
			//test toast to get selected text string
            Toast.makeText(context, selecPicker , Toast.LENGTH_SHORT).show();
          
        }
    });
	
	

Solution 5 - Android

This worked for me.

public class YourClassName extends AppCompatActivity {

NumberPicker picker = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout_file);

    picker = (NumberPicker) findViewById(R.id.pickerId_From_your_Layout_file);

     picker.setMinValue(0);
    picker.setMaxValue(3);
    picker.setDisplayedValues(new String[]{"English", "French","Kiswahili","عربى"});
}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontimoschloesserView Question on Stackoverflow
Solution 1 - AndroidnanView Answer on Stackoverflow
Solution 2 - AndroidharshitpthkView Answer on Stackoverflow
Solution 3 - AndroidFunkTheMonkView Answer on Stackoverflow
Solution 4 - AndroidIrvan Dwi PanggaView Answer on Stackoverflow
Solution 5 - AndroidNelsonRobertsView Answer on Stackoverflow