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();
}
Using custom views You can create a DialogFragment and display a dialog by overriding onCreateView() , either giving it a layoutId as you would with a typical fragment or using the DialogFragment constructor. The View returned by onCreateView() is automatically added to the dialog.
You need including 2 lines:
builder.setView(numberPicker);
return build.show()
So, the code must be like this:
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.setView(numberPicker);
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();
}
});
builder.create();
return builder.show();
}
You never set the view of the Dialog.
builder.setView(numberPicker);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With