0

Could someone give me an example of how to instantiate a NumberPickerDialog within Activity.onCreateDialog ?(https://github.com/novak/numberpicker/blob/master/lib/src/com/michaelnovakjr/numberpicker/NumberPickerDialog.java) ?

There are examples in a repo called numberpicker-demo for using the widget, but none for the actual dialog.

Amongst other approaches I've tried tried something like:

return new NumberPickerDialog.Builder(this)
    .setTitle("Choose Number")
    .etc..

But this just shows a standard AlertDialog, without the NumberPicker.

Thanks!

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
brk3
  • 1,524
  • 2
  • 19
  • 31

3 Answers3

3

Got it working eventually. There's an example in com.quietlycoding.android.picker.Picker, but I've found that the dialog doesn't set the dimming properly, blacking out the whole Activity in the background while it's in view.

I worked around this by simply creating an AlertDialog in the usual way, and then just sticking a NumberPicker widget into setView():

LayoutInflater inflater = (LayoutInflater)
    getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View npView = inflater.inflate(R.layout.number_picker_pref, null);
    return new AlertDialog.Builder(this)
        .setTitle("Text Size:")
        .setView(npView)
        .setPositiveButton(R.string.dialog_ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .setNegativeButton(R.string.dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
            .create();

Make sure to copy number_picker_pref.xml from the numberpicker project to res/layout in your own project.

brk3
  • 1,524
  • 2
  • 19
  • 31
0

That should be much simpler if only you do this:

  1. Add NumberPicker to you layout
  2. Add this code on your Activity

        //charger le NumberPicker
        npicker = (NumberPicker) findViewById(R.id.picker);
        // Set intervalle
        npicker.setRange(1, pages.size());
        // Set la valeur actuelle
        npicker.setCurrent(1);
    
        npicker.setOnChangeListener(new OnChangedListener() {               
            @Override
            public void onChanged(NumberPicker picker, int oldVal, int newVal) {
                // TODO Auto-generated method stub
                Log.e("Log Change event","oldVal: "+oldVal+"//newVal: "+newVal);
            }
        });
    
0

Look at this (and optionaly this: Creating dialogs).

Community
  • 1
  • 1
æ-ra-code
  • 2,140
  • 30
  • 28
  • I dont think you read my question properly. I'm aware of the existence of NumberPicker dialog and also how to create them dialogs in general. What I can't get right is how to create a NumberPickerDialog using the numberpicker library. – brk3 Jul 20 '11 at 14:01
  • Hmmm... Can you create NumberPickerDialog using library but without calling `Activity.onCreateDialog`? – æ-ra-code Jul 21 '11 at 08:29