I'm using number picker in a dialog and want to change the scroll direction from Up to down. Which mean currently by default if i scroll up, numbers come from bottoms side but i want them to come from upside and scroll will be downwards instead of upwards. Here is my number picker Dialog Code.
private static void getMeasure(int textMsg, final BoardRect item,
final int defaultValue, final int maxValue,
final OnUIMeasureReadListener listener) {
final NumberPicker picker = new NumberPicker(
AppContext.getActivityContext());
picker.setMinValue(-1);
picker.setMaxValue(maxValue);
picker.setWrapSelectorWheel(false);
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
// create actual dialog
final AlertDialog.Builder msgbox = new AlertDialog.Builder(
AppContext.getActivityContext());
msgbox.setCancelable(true);
msgbox.setTitle(AppContext.getActivityContext().getResources()
.getString(R.string.rect_dimen));
msgbox.setMessage(textMsg);
msgbox.setView(picker);
msgbox.setPositiveButton(AppContext.getActivityContext().getResources()
.getString(R.string.dlg_positive_btn),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
listener.measureRead(picker.getValue());
} catch (Exception ex) {
}
}
});
AlertDialog dialog = msgbox.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
wmlp.x = 135; // x position
wmlp.y = 0; // y position
dialog.getWindow().setAttributes(wmlp);
dialog.show();
dialog.getWindow().setLayout(350, 650);
}
I just had the same problem. I used the setDisplayedValues()
method of the NumberPicker
class to explicitly set the values to be displayed. You can generate an array of strings that represent the string values of the numbers you want:
public String[] getDisplayValues(int minimumInclusive, int maximumInclusive) {
ArrayList<String> result = new ArrayList<String>();
for(int i = maximumInclusive; i >= minimumInclusive; i--) {
result.add(Integer.toString(i));
}
return result.toArray(new String[0]);
}
Store that array in a field _displayValues
and then you can call:
picker.setDisplayValues(_displayValues);
//we want the max value to be the index of our last value
picker.setMaxValue(_displayValues.length - 1);
When the OnValueChangeListener
event is raised, use newVal
as an index into your array:
var realValue = Integer.parseInt(_displayValues[newVal]);
Hope that helps.