List of usage examples for android.widget NumberPicker setOnValueChangedListener
public void setOnValueChangedListener(OnValueChangeListener onValueChangedListener)
From source file:org.sufficientlysecure.keychain.ui.SafeSlingerActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.safe_slinger_activity); mMasterKeyId = getIntent().getLongExtra(EXTRA_MASTER_KEY_ID, 0); // NOTE: there are two versions of this layout, for API >= 11 and one for < 11 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { NumberPicker picker = (NumberPicker) findViewById(R.id.safe_slinger_picker); picker.setMinValue(2);// w w w . j av a 2 s . co m picker.setMaxValue(10); picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mSelectedNumber = newVal; } }); } else { Spinner spinner = (Spinner) findViewById(R.id.safe_slinger_spinner); List<String> list = new ArrayList<String>(); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); list.add("7"); list.add("8"); list.add("9"); list.add("10"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(dataAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mSelectedNumber = position + 2; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } ImageView buttonIcon = (ImageView) findViewById(R.id.safe_slinger_button_image); buttonIcon.setColorFilter(getResources().getColor(R.color.tertiary_text_light), PorterDuff.Mode.SRC_IN); View button = findViewById(R.id.safe_slinger_button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startExchange(mMasterKeyId, mSelectedNumber); } }); }
From source file:com.jaguarlandrover.hvacdemo.MainActivity.java
private void configurePicker(NumberPicker picker) { picker.setMinValue(15);/* www . j av a2 s .c o m*/ picker.setMaxValue(29); picker.setWrapSelectorWheel(false); picker.setDisplayedValues(new String[] { "LO", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "HI" }); picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { /* USER INTERFACE CALLBACK */ @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { Log.d(TAG, Util.getMethodName()); HVACManager.invokeService(getServiceIdentifiersFromViewId(picker.getId()), Integer.toString(newVal)); } }); }
From source file:com.untie.daywal.activity.MainActivity.java
private void showDayPicker() { Calendar calender = Calendar.getInstance(); pickedYear = calender.get(Calendar.YEAR); pickedMonth = calender.get(Calendar.MONTH) + 1; final Dialog dayPickerDialog = new Dialog(this); dayPickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dayPickerDialog.setContentView(R.layout.dialog); Button okBtn = (Button) dayPickerDialog.findViewById(R.id.birthday_btn_ok); Button cancelBtn = (Button) dayPickerDialog.findViewById(R.id.birthday_btn_cancel); dialog_year_title = (TextView) dayPickerDialog.findViewById(R.id.dialog_year_title); dialog_month_title = (TextView) dayPickerDialog.findViewById(R.id.dialog_month_title); final NumberPicker yearPicker = (NumberPicker) dayPickerDialog.findViewById(R.id.yearPicker); final NumberPicker monthPicker = (NumberPicker) dayPickerDialog.findViewById(R.id.monthPicker); dialog_year_title.setText(String.valueOf(pickedYear) + ""); dialog_month_title.setText(String.valueOf(pickedMonth) + ""); yearPicker.setMinValue(pickedYear - 100); yearPicker.setMaxValue(pickedYear + 40); yearPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); // setDividerColor(yearPicker, android.R.color.white ); yearPicker.setWrapSelectorWheel(false); yearPicker.setValue(pickedYear);/* w ww .j a v a 2s.c o m*/ yearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { dialog_year_title.setText(String.valueOf(newVal) + ""); } }); monthPicker.setMinValue(1); monthPicker.setMaxValue(12); monthPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); // setDividerColor(monthPicker, android.R.color.white); //monthPicker.setWrapSelectorWheel(false); monthPicker.setValue(pickedMonth); monthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { dialog_month_title.setText(String.valueOf(newVal) + ""); } }); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //TODO : ?? yearPicker.getValue() monthPicker.getValue() //Toast.makeText(MainActivity.this, String.valueOf(yearPicker.getValue())+"/"+monthPicker.getValue(), Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, MainActivity.class); intent.putExtra("year", yearPicker.getValue()); intent.putExtra("month", monthPicker.getValue()); intent.putExtra("order", 1); dayPickerDialog.dismiss(); startActivity(intent); overridePendingTransition(0, 0); } }); cancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dayPickerDialog.dismiss(); } }); dayPickerDialog.show(); }
From source file:com.crcrch.chromatictuner.app.NotePickerFragment.java
@NonNull @Override/*from w w w . jav a 2 s.c om*/ public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext()); View dialogContent = LayoutInflater.from(dialogBuilder.getContext()).inflate(R.layout.fragment_note_picker, null); final NumberPicker notePicker = (NumberPicker) dialogContent.findViewById(R.id.picker_note); notePicker.setMinValue(0); notePicker.setMaxValue(MiscMusic.CHROMATIC_SCALE.length - 1); notePicker.setDisplayedValues(MiscMusic.CHROMATIC_SCALE); notePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); final NumberPicker octavePicker = (NumberPicker) dialogContent.findViewById(R.id.picker_octave); octavePicker.setMinValue(0); octavePicker.setMaxValue(MAX_OCTAVE); octavePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); octavePicker.setWrapSelectorWheel(false); final TextView frequencyView = (TextView) dialogContent.findViewById(R.id.frequency); notePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { frequencyView.setText(selectFrequency(newVal, octavePicker.getValue())); } }); octavePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { frequencyView.setText(selectFrequency(notePicker.getValue(), newVal)); } }); double frequencyToDisplay; if (savedInstanceState == null) { frequencyToDisplay = initialFrequency; } else { frequencyToDisplay = savedInstanceState.getDouble(STATE_FREQUENCY); } frequencyView.setText(selectFrequency(frequencyToDisplay)); int note = getNoteForFrequency(frequencyToDisplay); octavePicker.setValue(getOctaveForNote(note)); octavePicker.invalidate(); notePicker.setValue(getNoteIndexForNote(note)); notePicker.invalidate(); return dialogBuilder.setTitle(R.string.dialog_title_note_picker).setView(dialogContent) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { listener.onFrequencySelected(frequency); dialog.dismiss(); } }).setNegativeButton(android.R.string.cancel, null).create(); }
From source file:org.tvbrowser.tvbrowser.TvBrowser.java
private void sortChannels() { ContentResolver cr = getContentResolver(); StringBuilder where = new StringBuilder(TvBrowserContentProvider.CHANNEL_KEY_SELECTION); where.append("=1"); LinearLayout main = (LinearLayout) getLayoutInflater().inflate(R.layout.channel_sort_list, getParentViewGroup(), false); Button sortAlphabetically = (Button) main.findViewById(R.id.channel_sort_alpabetically); final DynamicListView channelSort = (DynamicListView) main.findViewById(R.id.channel_sort); String[] projection = { TvBrowserContentProvider.KEY_ID, TvBrowserContentProvider.CHANNEL_KEY_NAME, TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER, TvBrowserContentProvider.CHANNEL_KEY_SELECTION, TvBrowserContentProvider.CHANNEL_KEY_LOGO }; Cursor channels = cr.query(TvBrowserContentProvider.CONTENT_URI_CHANNELS, projection, where.toString(), null, TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER); final ArrayList<SortInterface> channelSource = new ArrayList<SortInterface>(); if (channels.moveToFirst()) { do {//from w w w. ja v a2s. c o m int key = channels.getInt(0); String name = channels.getString(1); int order = 0; if (!channels.isNull(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER))) { order = channels .getInt(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER)); } Bitmap channelLogo = UiUtils.createBitmapFromByteArray( channels.getBlob(channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_LOGO))); if (channelLogo != null) { BitmapDrawable l = new BitmapDrawable(getResources(), channelLogo); ColorDrawable background = new ColorDrawable(SettingConstants.LOGO_BACKGROUND_COLOR); background.setBounds(0, 0, channelLogo.getWidth() + 2, channelLogo.getHeight() + 2); LayerDrawable logoDrawable = new LayerDrawable(new Drawable[] { background, l }); logoDrawable.setBounds(background.getBounds()); l.setBounds(2, 2, channelLogo.getWidth(), channelLogo.getHeight()); channelLogo = UiUtils.drawableToBitmap(logoDrawable); } channelSource.add(new ChannelSort(key, name, order, channelLogo)); } while (channels.moveToNext()); channels.close(); final Comparator<SortInterface> sortComparator = new Comparator<SortInterface>() { @Override public int compare(SortInterface lhs, SortInterface rhs) { if (lhs.getSortNumber() < rhs.getSortNumber()) { return -1; } else if (lhs.getSortNumber() > rhs.getSortNumber()) { return 1; } return 0; } }; Collections.sort(channelSource, sortComparator); // create default logo for channels without logo final Bitmap defaultLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); final StableArrayAdapter<SortInterface> aa = new StableArrayAdapter<SortInterface>(TvBrowser.this, R.layout.channel_sort_row, channelSource) { public View getView(int position, View convertView, ViewGroup parent) { ChannelSort value = (ChannelSort) getItem(position); ViewHolder holder = null; if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) getContext() .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.channel_sort_row, getParentViewGroup(), false); holder.mTextView = (TextView) convertView.findViewById(R.id.row_of_channel_sort_text); holder.mSortNumber = (TextView) convertView.findViewById(R.id.row_of_channel_sort_number); holder.mLogo = (ImageView) convertView.findViewById(R.id.row_of_channel_sort_icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.mTextView.setText(value.getName()); String sortNumber = String.valueOf(value.getSortNumber()); if (value.getSortNumber() == 0) { sortNumber = "-"; } sortNumber += "."; holder.mSortNumber.setText(sortNumber); Bitmap logo = value.getLogo(); if (logo != null) { holder.mLogo.setImageBitmap(logo); } else { holder.mLogo.setImageBitmap(defaultLogo); } return convertView; } }; channelSort.setAdapter(aa); channelSort.setArrayList(channelSource); channelSort.setSortDropListener(new SortDropListener() { @Override public void dropped(int originalPosition, int position) { int startIndex = originalPosition; int endIndex = position; int droppedPos = position; if (originalPosition > position) { startIndex = position; endIndex = originalPosition; } int previousNumber = 0; if (startIndex > 0) { previousNumber = aa.getItem(startIndex - 1).getSortNumber(); } int firstVisible = channelSort.getFirstVisiblePosition(); for (int i = startIndex; i <= endIndex; i++) { if (i == droppedPos || aa.getItem(i).getSortNumber() != 0) { aa.getItem(i).setSortNumber(++previousNumber); if (i >= firstVisible) { View line = channelSort.getChildAt(i - firstVisible); if (line != null) { ((TextView) line.findViewById(R.id.row_of_channel_sort_number)) .setText(String.valueOf(previousNumber) + "."); } } } } } }); channelSort.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this); LinearLayout numberSelection = (LinearLayout) getLayoutInflater() .inflate(R.layout.sort_number_selection, getParentViewGroup(), false); mSelectionNumberChanged = false; final NumberPicker number = (NumberPicker) numberSelection.findViewById(R.id.sort_picker); number.setMinValue(1); number.setMaxValue(channelSource.size()); number.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); number.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mSelectionNumberChanged = true; } }); final EditText numberAlternative = (EditText) numberSelection .findViewById(R.id.sort_entered_number); builder.setView(numberSelection); final ChannelSort selection = (ChannelSort) channelSource.get(position); TextView name = (TextView) numberSelection.findViewById(R.id.sort_picker_channel_name); name.setText(selection.getName()); if (selection.getSortNumber() > 0) { if (selection.getSortNumber() < channelSource.size() + 1) { number.setValue(selection.getSortNumber()); } else { numberAlternative.setText(String.valueOf(selection.getSortNumber())); } } builder.setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String test = numberAlternative.getText().toString().trim(); if (test.length() == 0 || mSelectionNumberChanged) { selection.setSortNumber(number.getValue()); } else { try { selection.setSortNumber(Integer.parseInt(test)); } catch (NumberFormatException e1) { } } Collections.sort(channelSource, sortComparator); aa.notifyDataSetChanged(); } }); builder.setNegativeButton(android.R.string.cancel, null); builder.show(); } }); sortAlphabetically.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Collections.sort(channelSource, new Comparator<SortInterface>() { @Override public int compare(SortInterface lhs, SortInterface rhs) { return lhs.getName().compareToIgnoreCase(rhs.getName()); } }); for (int i = 0; i < channelSource.size(); i++) { channelSource.get(i).setSortNumber(i + 1); } aa.notifyDataSetChanged(); } }); AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this); builder.setTitle(R.string.action_sort_channels); builder.setView(main); builder.setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { boolean somethingChanged = false; for (SortInterface selection : channelSource) { if (((ChannelSort) selection).wasChanged()) { somethingChanged = true; ContentValues values = new ContentValues(); values.put(TvBrowserContentProvider.CHANNEL_KEY_ORDER_NUMBER, selection.getSortNumber()); getContentResolver().update( ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS, ((ChannelSort) selection).getKey()), values, null, null); } } if (somethingChanged) { updateProgramListChannelBar(); } } }); builder.setNegativeButton(android.R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }
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 ww w . j a va2 s. co 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; }