Example usage for android.widget NumberPicker getMaxValue

List of usage examples for android.widget NumberPicker getMaxValue

Introduction

In this page you can find the example usage for android.widget NumberPicker getMaxValue.

Prototype

public int getMaxValue() 

Source Link

Document

Returns the max value of the picker.

Usage

From source file:butter.droid.base.fragments.dialog.NumberPickerDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    if (getArguments() == null || !getArguments().containsKey(MAX_VALUE)
            || !getArguments().containsKey(MIN_VALUE) || !getArguments().containsKey(TITLE)
            || mOnResultListener == null) {
        return builder.create();
    }// www  .  j a  v a  2 s .c o m

    final NumberPicker numberPicker = new NumberPicker(getActivity());
    numberPicker.setLayoutParams(new NumberPicker.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    numberPicker.setWrapSelectorWheel(false);

    final int minValue = getArguments().getInt(MIN_VALUE);
    final int maxValue = getArguments().getInt(MAX_VALUE);
    final int currentValue = getArguments().getInt(DEFAULT_VALUE,
            (int) Math.floor((numberPicker.getMaxValue() - numberPicker.getMinValue()) / 2));

    List<String> displayValues = new ArrayList<>();
    for (int i = minValue; i < maxValue + 1; i++) {
        displayValues.add(Integer.toString(i));
    }
    numberPicker.setDisplayedValues(displayValues.toArray(new String[displayValues.size()]));

    if (minValue < 0) {
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(maxValue + Math.abs(minValue));
        numberPicker.setValue(currentValue + Math.abs(minValue));
    } else {
        numberPicker.setMinValue(minValue);
        numberPicker.setMaxValue(maxValue);
        numberPicker.setValue(currentValue);
    }

    if (getArguments().containsKey(FOCUSABLE) && !getArguments().getBoolean(FOCUSABLE))
        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    builder.setView(numberPicker).setTitle(getArguments().getString(TITLE))
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mOnResultListener.onNewValue(numberPicker.getValue() + (minValue < 0 ? minValue : 0));
                    dialog.dismiss();
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    return builder.create();
}