Question

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();
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top