Forum Xamarin.Android

How do I create a Numberpicker?

Bass31Bass31 USMember ✭✭

Hi Guys,

I'm using a time picker to based on this example: http://docs.xamarin.com/guides/android/user_interface/time_picker
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 == TIME_DIALOG_ID)
return new TimePickerDialog (this, TimePickerCallback, hour, minute, false);

    return null;

}

Posts

  • CheesebaronCheesebaron DKInsider, University mod

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

  • Bass31Bass31 USMember ✭✭

    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?

  • FZelleFZelle DEMember ✭✭✭

    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.

  • Bass31Bass31 USMember ✭✭

    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 :)

  • CheesebaronCheesebaron DKInsider, University mod
        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

  • FZelleFZelle DEMember ✭✭✭

    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.

  • Bass31Bass31 USMember ✭✭

    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

  • CheesebaronCheesebaron DKInsider, University mod

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

  • ChristopherDrososChristopherDrosos GRMember ✭✭
    edited March 2015

    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

  • ChristopherDrososChristopherDrosos GRMember ✭✭

    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 (); });
    
Sign In or Register to comment.