Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberPicker is not showing in AlertDialog

I'm trying to add a NumberPicker to an AlertDialog, but it does not apper even though there seems to be no error. I'm pretty newbie at Android coding so I assume it's something really stupid that's missing there.

        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        alert.setTitle("Select the value: ");

        NumberPicker np = new NumberPicker(MainActivity.this);
        String[] nums = new String[100];
        for(int i=0; i<nums.length; i++)
               nums[i] = Integer.toString(i);

        np.setMinValue(1);
        np.setMaxValue(nums.length-1);
        np.setWrapSelectorWheel(false);
        np.setDisplayedValues(nums);
        np.setValue(50);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          // Do something with value!
          }
        });

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

        alert.show();
like image 628
Paradoxis Avatar asked Oct 05 '22 12:10

Paradoxis


1 Answers

Add alert.setView(np); You have to tell alert dialog to set new view.

like image 104
Vasudev Avatar answered Oct 10 '22 01:10

Vasudev