Search code examples
androidandroid-alertdialognumberpicker

Display a NumberPicker on an AlertDialog


I am trying to display a NumberPicker on an AlertDialog.
The AlertDialog works, but it doesn't show the NumberPicker.

Here is my code

public Dialog onCreateDialog(Bundle savedInstanceState){
    final NumberPicker numberPicker = new NumberPicker(getActivity());
    numberPicker.setMaxValue(360);
    numberPicker.setMinValue(0);


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Changing the Hue");
    builder.setMessage("Choose a value :");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialogHost.onPositiveButton(numberPicker.getValue());
        }
    });
    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialogHost.onCancelButton();
        }
    });
    return builder.create();
}

Solution

  • You never set the view of the Dialog.

    builder.setView(numberPicker);