Example usage for android.widget NumberPicker NumberPicker

List of usage examples for android.widget NumberPicker NumberPicker

Introduction

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

Prototype

public NumberPicker(Context context) 

Source Link

Document

Create a new number picker.

Usage

From source file:com.matthewmitchell.wakeifyplus.NumberPickerFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final NumberPicker np = new NumberPicker(activity);
    np.setMaxValue(60);// w w w  .j av a2s  .  c o  m
    np.setMinValue(1);
    np.setValue(defaultNum);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Set Snooze Minutes").setView(np)
            .setPositiveButton("Set", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    activity.snoozeMinutes = np.getValue();
                    TextView edit = (TextView) activity.findViewById(R.id.snooze_edit);
                    edit.setText("" + activity.snoozeMinutes);
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Do nothing.
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();

}

From source file:com.andremion.louvre.sample.NumberPickerDialog.java

@NonNull
@Override//w  ww.  ja  v a 2s .co  m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mNumberPicker = new NumberPicker(getContext());
    mNumberPicker.setMinValue(MIN_VALUE);
    mNumberPicker.setMaxValue(MAX_VALUE);
    mNumberPicker.setValue(INITIAL_VALUE);
    mNumberPicker.setWrapSelectorWheel(true);
    return new AlertDialog.Builder(getContext()).setTitle(R.string.max_select_prompt).setView(mNumberPicker)
            .setPositiveButton(android.R.string.ok, this).setNegativeButton(android.R.string.cancel, null)
            .create();
}

From source file:com.matthewmitchell.wakeifyplus.MinutesSecondsFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    TableRow.LayoutParams twoLP = new TableRow.LayoutParams(0, 0, 0.2f);
    TableRow.LayoutParams threeLP = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT, 0.3f);

    View spacer = new View(activity);
    spacer.setLayoutParams(twoLP);/*from  w  ww  . j  a v a2  s  .c  om*/
    spacer.setVisibility(View.INVISIBLE);

    final NumberPicker minutes = new NumberPicker(activity);
    minutes.setMaxValue(30);
    minutes.setMinValue(0);
    minutes.setValue(defaultMinute);
    minutes.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    LinearLayout minutesLayout = new LinearLayout(activity);
    minutesLayout.addView(minutes);
    minutesLayout.setGravity(Gravity.CENTER);
    minutesLayout.setLayoutParams(threeLP);

    final NumberPicker seconds = new NumberPicker(activity);
    seconds.setMaxValue(59);
    seconds.setMinValue(0);
    seconds.setValue(defaultSecond);
    seconds.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    LinearLayout secondsLayout = new LinearLayout(activity);
    secondsLayout.addView(seconds);
    secondsLayout.setGravity(Gravity.CENTER);
    secondsLayout.setLayoutParams(threeLP);

    LinearLayout layout = new LinearLayout(activity);
    layout.addView(spacer);
    layout.addView(minutesLayout);
    layout.addView(secondsLayout);
    layout.setWeightSum(1.0f);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Set Volume Ramping Time").setView(layout)
            .setPositiveButton("Set", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    activity.rampingMinutes = minutes.getValue();
                    activity.rampingSeconds = seconds.getValue();
                    TextView edit = (TextView) activity.findViewById(R.id.volume_ramping);
                    edit.setText(activity.rampingMinutes + "m" + activity.rampingSeconds + "s");
                }

            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Do nothing.
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();

}

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();
    }//from   www .  jav  a 2 s  . c  om

    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();
}

From source file:ca.ualberta.slevinsk.gameshow.NumberPickerFragment.java

@NonNull
@Override//from  w ww.  j av  a2s .  c  o  m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("How many players?");

    builder.setCancelable(false);
    numberPicker = new NumberPicker(getContext());
    numberPicker.setMinValue(2);
    numberPicker.setMaxValue(4);
    numberPicker.setOnValueChangedListener(onPlayerCountChangeListener);

    builder.setView(numberPicker);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    return builder.create();
}

From source file:com.yacorso.nowaste.views.adapters.FridgeFoodAdapter.java

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    final FridgeViewHolder holder = (FridgeViewHolder) viewHolder;

    final Food food = foodList.get(position);

    final int quantity = food.getFoodFridge().getQuantity();

    holder.tvName.setText(food.getName());
    holder.btnQuantity.setText(Integer.toString(quantity));
    Date outOfDate = food.getFoodFridge().getOutOfDate();
    holder.outOfDate.setText(getDateTextFromDate(outOfDate));
    setOpenIcon(holder.btnOpenToggle, food);

    setColorCircleFromDate(holder.btnQuantity, outOfDate, mContext.getResources());

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override//ww w.ja  va2s .  c o  m
        public void onClick(View v) {

            lastFoodClickedPosition = position;
            switch (v.getId()) {
            case R.id.btn_food_quantity:
                /*
                 *  Popup changement quantit
                 */
                final NumberPicker numberPicker = new NumberPicker(mContext);
                numberPicker.setMinValue(1);
                numberPicker.setMaxValue(100);
                numberPicker.setValue(quantity);
                numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage(R.string.title_quantity_number_picker);
                builder.setView(numberPicker);

                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*
                         * Save the modification
                         */
                        int selectedValue = numberPicker.getValue();
                        food.getFoodFridge().setQuantity(selectedValue);
                        mFoodProvider.update(food);
                    }
                });

                builder.setNegativeButton(android.R.string.cancel, null);

                builder.create().show();
                break;

            case R.id.btn_open_toggle:
                /*
                 *  Open/close food
                 */
                FoodFridge foodFridge = food.getFoodFridge();
                foodFridge.toggleOpen();
                setOpenIcon(v, food);
                Date outOfDate = foodFridge.getOutOfDate();
                setColorCircleFromDate(holder.btnQuantity, outOfDate, v.getResources());
                mFoodProvider.update(food);
                break;

            case R.id.item_text_zone:
                EventBus.getDefault().post(new CallUpdateFoodEvent(food));
                break;
            case R.id.btn_favorite_toggle:
                EventBus.getDefault().post(new CallAddFoodToCustomListEvent(food));
                break;
            }
        }
    };

    holder.btnQuantity.setOnClickListener(clickListener);
    holder.btnOpenToggle.setOnClickListener(clickListener);
    holder.btnFavoriteToggle.setOnClickListener(clickListener);
    holder.textZone.setOnClickListener(clickListener);
}

From source file:org.ciasaboark.tacere.activity.fragment.AdvancedSettingsFragment.java

private void drawEventBufferWidgets() {
    LinearLayout bufferBox = (LinearLayout) rootView.findViewById(R.id.advanced_settings_buffer_box);
    bufferBox.setOnClickListener(new View.OnClickListener() {
        @Override//from  w w  w  .ja v a 2  s.  c  om
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(context, R.style.Dialog));
            builder.setTitle("Buffer Minutes");
            final NumberPicker number = new NumberPicker(context);
            String[] nums = new String[32];

            for (int i = 0; i < nums.length; i++) {
                nums[i] = Integer.toString(i);
            }

            number.setMinValue(1);
            number.setMaxValue(nums.length - 1);
            number.setWrapSelectorWheel(false);
            number.setDisplayedValues(nums);
            number.setValue(prefs.getBufferMinutes() + 1);

            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    prefs.setBufferMinutes(number.getValue() - 1);
                    drawEventBufferWidgets();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Do nothing
                }
            });

            builder.setView(number);

            AlertDialog dialog = builder.show();
        }
    });

    // the event buffer button
    TextView bufferTV = (TextView) rootView.findViewById(R.id.bufferMinutesDescription);
    String bufferText = getResources().getString(R.string.advanced_settings_section_intervals_buffer_duration);
    bufferTV.setText(String.format(bufferText, prefs.getBufferMinutes()));
}

From source file:cz.maresmar.sfm.view.menu.MenuDetailsFragment.java

private void showPickerDialog(@StringRes int title, int min, int max, int value, IntConsumer newValueConsumer) {
    // Prepare number picker
    NumberPicker picker = new NumberPicker(getContext());
    picker.setMinValue(min);/* w  w w. j av a  2  s .  c  o  m*/
    picker.setMaxValue(max);
    picker.setValue(value);
    picker.setWrapSelectorWheel(false);

    FrameLayout layout = new FrameLayout(getContext());
    layout.addView(picker, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));

    // Show alert
    new AlertDialog.Builder(getContext()).setTitle(title).setMessage(R.string.menu_detail_pick_new_value)
            .setView(layout).setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
                // do something with picker.getValue()
                newValueConsumer.accept(picker.getValue());
            }).setNegativeButton(android.R.string.cancel, null).show();
}

From source file:io.github.data4all.activity.MapViewActivity.java

private void bodyheightdialog() {
    PreferenceManager.setDefaultValues(this, R.xml.settings, false);
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
    firstUse = userPrefs.getBoolean("firstUse", true);

    if (firstUse) {
        RelativeLayout linearLayout = new RelativeLayout(this);
        final NumberPicker numberPicker = new NumberPicker(this);
        numberPicker.setMaxValue(250);// www .  j  a v  a  2s.com
        numberPicker.setMinValue(80);
        numberPicker.setValue(180);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
        RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        linearLayout.setLayoutParams(params);
        linearLayout.addView(numberPicker, numPicerParams);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(R.string.pref_bodyheight_dialog_title);
        alertDialogBuilder.setMessage(R.string.pref_bodyheight_dialog_message);
        alertDialogBuilder.setView(linearLayout);
        alertDialogBuilder.setCancelable(false).setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Log.d(TAG, "set bodyheight to: " + numberPicker.getValue());
                        prefs.edit().putString("PREF_BODY_HEIGHT", String.valueOf(numberPicker.getValue()))
                                .commit();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        // set firstUse to false so this dialog is not shown again. ever.
        userPrefs.edit().putBoolean("firstUse", false).commit();
        firstUse = false;
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Object showNativePicker(final int type, final Component source, final Object currentValue,
        final Object data) {
    if (getActivity() == null) {
        return null;
    }//from  w  w w.j  a v  a  2s.c  o  m
    final boolean[] canceled = new boolean[1];
    final boolean[] dismissed = new boolean[1];

    if (editInProgress()) {
        stopEditing(true);
    }
    if (type == Display.PICKER_TYPE_TIME) {

        class TimePick
                implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnCancelListener, Runnable {
            int result = ((Integer) currentValue).intValue();

            public void onTimeSet(TimePicker tp, int hour, int minute) {
                result = hour * 60 + minute;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            @Override
            public void onCancel(DialogInterface di) {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final TimePick pickInstance = new TimePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                int hour = ((Integer) currentValue).intValue() / 60;
                int minute = ((Integer) currentValue).intValue() % 60;
                TimePickerDialog tp = new TimePickerDialog(getActivity(), pickInstance, hour, minute, true) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                //DateFormat.is24HourFormat(activity));
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        return new Integer(pickInstance.result);
    }
    if (type == Display.PICKER_TYPE_DATE) {
        final java.util.Calendar cl = java.util.Calendar.getInstance();
        if (currentValue != null) {
            cl.setTime((Date) currentValue);
        }
        class DatePick
                implements DatePickerDialog.OnDateSetListener, DatePickerDialog.OnCancelListener, Runnable {
            Date result = (Date) currentValue;

            public void onDateSet(DatePicker dp, int year, int month, int day) {
                java.util.Calendar c = java.util.Calendar.getInstance();
                c.set(java.util.Calendar.YEAR, year);
                c.set(java.util.Calendar.MONTH, month);
                c.set(java.util.Calendar.DAY_OF_MONTH, day);
                result = c.getTime();
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void onCancel(DialogInterface di) {
                result = null;
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final DatePick pickInstance = new DatePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                DatePickerDialog tp = new DatePickerDialog(getActivity(), pickInstance,
                        cl.get(java.util.Calendar.YEAR), cl.get(java.util.Calendar.MONTH),
                        cl.get(java.util.Calendar.DAY_OF_MONTH)) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        return pickInstance.result;
    }
    if (type == Display.PICKER_TYPE_STRINGS) {
        final String[] values = (String[]) data;
        class StringPick implements Runnable, NumberPicker.OnValueChangeListener {
            int result = -1;

            StringPick() {
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void cancel() {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void ok() {
                canceled[0] = false;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            @Override
            public void onValueChange(NumberPicker np, int oldVal, int newVal) {
                result = newVal;
            }
        }

        final StringPick pickInstance = new StringPick();
        for (int iter = 0; iter < values.length; iter++) {
            if (values[iter].equals(currentValue)) {
                pickInstance.result = iter;
                break;
            }
        }
        if (pickInstance.result == -1 && values.length > 0) {
            // The picker will default to showing the first element anyways
            // If we don't set the result to 0, then the user has to first
            // scroll to a different number, then back to the first option
            // to pick the first option.
            pickInstance.result = 0;
        }

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                NumberPicker picker = new NumberPicker(getActivity());
                if (source.getClientProperty("showKeyboard") == null) {
                    picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
                }
                picker.setMinValue(0);
                picker.setMaxValue(values.length - 1);
                picker.setDisplayedValues(values);
                picker.setOnValueChangedListener(pickInstance);
                if (pickInstance.result > -1) {
                    picker.setValue(pickInstance.result);
                }
                RelativeLayout linearLayout = new RelativeLayout(getActivity());
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
                RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

                linearLayout.setLayoutParams(params);
                linearLayout.addView(picker, numPicerParams);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
                alertDialogBuilder.setView(linearLayout);
                alertDialogBuilder.setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                pickInstance.ok();
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                pickInstance.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        if (pickInstance.result < 0) {
            return null;
        }
        return values[pickInstance.result];
    }
    return null;
}