1

I have a problem with NumberPicker. I've created a Dialog in which I want to put a NumberPicker. The NumberPicker displays, but I can't change a number in it. I've set MinValue and MaxValue and I can't figure aout what the problem is.

SettingsDialogFragment.java

public class SettingsDialogFragment extends DialogFragment{

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.settings);
        builder.setView(inflater.inflate(R.layout.settings_dialog, null))
            .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   }
               })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   }
               });
        final NumberPicker np = (NumberPicker) inflater.inflate(R.layout.settings_dialog, null).findViewById(R.id.weight_picker);
        np.setMaxValue(300);
        np.setMinValue(0);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(( new NumberPicker.
        OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("value is",""+newVal);
            }
        }));
        return builder.create();
    }
}

settings_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <NumberPicker 
        android:id="@+id/weight_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

</LinearLayout>

Screenshot

screenshot with dialog and numberpicker

fragon
  • 3,391
  • 10
  • 39
  • 76
  • 1
    try this link:http://stackoverflow.com/questions/17944061/how-to-use-number-picker-with-dialog – prakash Sep 18 '14 at 12:52

1 Answers1

3

Try this way,hope this will help you to solve your problem.

public class SettingsDialogFragment extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.settings);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.settings_dialog, null);

        builder.setView(view);
        builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
         NumberPicker np = (NumberPicker) view.findViewById(R.id.weight_picker);
        np.setMaxValue(300);
        np.setMinValue(0);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(( new NumberPicker.
                OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("value is",""+newVal);
            }
        }));
        return builder.create();
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • It works. Could you please briefly explain me why my code did not work? – fragon Sep 18 '14 at 13:17
  • 1
    @6franek,Sure i think your code is not working becz you have inflate your layout two time once at setView() another at find number picker so both taken two difference reference to xml and you given first layout inflation reference to setView which is show in dialog and second layout inflation reference to implement setOnValueChangedListener for listener number picker which indirectly reference another one not dialog view so you can not getting number picker onValueChange in dialog view. – Haresh Chhelana Sep 19 '14 at 04:13