Reputation: 3471
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.
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();
}
}
<?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>
Upvotes: 1
Views: 1549
Reputation: 24848
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();
}
}
Upvotes: 3