locked
How do I create a Numberpicker? RRS feed

  • Question

  • User20374 posted

    Hi Guys,

    I'm using a time picker to based on this example: http://docs.xamarin.com/guides/android/userinterface/timepicker Everything works fine, but I also need a number picker. Can I do that the same way as the time picker? I tried a few things, but without succes. Below are the code snippets copied from the page metioned above.

    Thanks!

    protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle);

        // Set our view from the "Main" layout resource
        SetContentView (Resource.Layout.Main);
    
        // Capture our View elements
        time_display = FindViewById<TextView> (Resource.Id.timeDisplay);
        pick_button = FindViewById<Button> (Resource.Id.pickTime);
    
        // Add a click listener to the button
        pick_button.Click += (o, e) => ShowDialog (TIME_DIALOG_ID);
    
        // Get the current time
        hour = DateTime.Now.Hour;
        minute = DateTime.Now.Minute;
    
        // Display the current date
        UpdateDisplay ();
    

    }

    // Updates the time we display in the TextView private void UpdateDisplay () { string time = string.Format ("{0}:{1}", hour, minute.ToString ().PadLeft (2, '0')); time_display.Text = time; }

    private void TimePickerCallback (object sender, TimePickerDialog.TimeSetEventArgs e) { hour = e.HourOfDay; minute = e.Minute; UpdateDisplay (); }

    protected override Dialog OnCreateDialog (int id) { if (id == TIMEDIALOGID) return new TimePickerDialog (this, TimePickerCallback, hour, minute, false);

        return null;
    

    }

    Monday, October 21, 2013 8:36 PM

All replies

  • User209 posted

    Use the NumberPicker widget, it is what TimePicker consists of and works almost the same.

    Monday, October 21, 2013 9:01 PM
  • User20374 posted

    Thanks,

    I cant find the NumberPicker, I only see the DataPicker and the TimePicker. (I'm using Visual Studio btw)

    But I don't think I need the widget. The widgets are placed directly on the screen, but I need the NumberPicker when I push a button, just like the TimePicker in the example mentioned earlier.

    Isn't it possible to modify the code above to NumberPicker?

    Tuesday, October 22, 2013 7:40 AM
  • User565 posted

    Numberpicker is a control (in Java called widget) exactly like DatePicker.

    And if you would put "numberpicker android example" into google you get hundrets of examples.

    Tuesday, October 22, 2013 8:10 AM
  • User20374 posted

    I don't use java, I'm using Visual Studio with a Xamarin plugin and it is C#.

    As I said before, I don't have a NumberPicker control within the toolbox, only the Date- and TimePicker. A lot of examples are in Java or use the NumberPicker control which I somehow don't have...

    In the code above it is also not necessary to use the TimePicker control, it creates it programmatically. I want to create the NumberPicker in a similar way as I created the TimePicker.

    If somebody can give me a link to a good example, that would be nice.

    I must say I'm a complete Newbie, so maybe I just don't get it :)

    Tuesday, October 22, 2013 9:05 AM
  • User209 posted
        private class NumberPickerDialogFragment : DialogFragment
        {
            private readonly Context _context;
            private readonly int _min, _max, _current;
            private readonly NumberPicker.IOnValueChangeListener _listener;
    
            public NumberPickerDialogFragment(Context context, int min, int max, int current, NumberPicker.IOnValueChangeListener listener)
            {
                _context = context;
                _min = min;
                _max = max;
                _current = current;
                _listener = listener;
            }
    
            public override Dialog OnCreateDialog(Bundle savedState)
            {
                var inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
                var view = inflater.Inflate(Resource.Layout.NumberPickerDialog, null);
                var numberPicker = view.FindViewById<NumberPicker>(Resource.Id.numberPicker);
                numberPicker.MaxValue = _max;
                numberPicker.MinValue = _min;
                numberPicker.Value = _current;
                numberPicker.SetOnValueChangedListener(_listener);
    
                var dialog = new AlertDialog.Builder(_context);
                dialog.SetTitle(Resource.String.number_dialog_title);
                dialog.SetView(view);
                dialog.SetNegativeButton(Resource.String.dialog_cancel, (s, a) => { });
                dialog.SetPositiveButton(Resource.String.dialog_ok, (s, a) => { });
                return dialog.Create();
            }
        }
    

    Resource/Layout/NumberPickerDialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <NumberPicker
        android:id="@+id/numberPicker"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center" />
    </LinearLayout>
    

    It is there, I have a sample with different Dialogs here: https://github.com/Cheesebaron/MonoDroid.DialogSample

    More on it in this blog post: http://blog.ostebaronen.dk/2013/02/using-dialogs-in-mono-for-android.html

    Tuesday, October 22, 2013 9:18 AM
  • User565 posted

    I must say I'm a complete Newbie, so maybe I just don't get it :)

    Yes, you don't get that the Toolbox is not filled with all controls.

    Tuesday, October 22, 2013 11:18 AM
  • User20374 posted

    Yes, you don't get that the Toolbox is not filled with all controls.

    What do you mean exactly?

    @Cheesebaron, thanks for the example. I hope I can use this for my app :P

    Tuesday, October 22, 2013 12:55 PM
  • User209 posted

    The toolbox in the Designer does not contain all the possible Views. That is what he means.

    Tuesday, October 22, 2013 12:58 PM
  • User46127 posted

    Hello @Cheesebaron nice codes here, i would like to ask something about the numberpicker dialog, If we have more than one numberpicker objects how we can save both of the values? When i have many seekbars with the same listener for example i know which one has change with this line:

     if (seekBar.Id == Resource.Id.MusicSeekBar) {
    

    How i can do something like this With the NumberPicker dialog? Thanks

    EDIT: ok i think is if (picker.Id = Resource.Id.numberPicker1) on OnValueChange Sorry :D

    Sunday, March 8, 2015 5:08 PM
  • User46127 posted

    In your NumberPickerDialogFragment code how i can run a function on OK press? Im getting this:

     cannot access a nonstatic member of outer type  "MyFragment" via nested type "NumberPickerDialogFragment "
    

    when im trying this

    dialog.SetPositiveButton("OK", (s, a) => { Update (); });
    
    Sunday, March 8, 2015 6:48 PM