For kotlin lovers.
fun numberPickerCustom() {
val d = AlertDialog.Builder(context)
val inflater = this.layoutInflater
val dialogView = inflater.inflate(R.layout.number_picker_dialog, null)
d.setTitle("Title")
d.setMessage("Message")
d.setView(dialogView)
val numberPicker = dialogView.findViewById<NumberPicker>(R.id.dialog_number_picker)
numberPicker.maxValue = 15
numberPicker.minValue = 1
numberPicker.wrapSelectorWheel = false
numberPicker.setOnValueChangedListener { numberPicker, i, i1 -> println("onValueChange: ") }
d.setPositiveButton("Done") { dialogInterface, i ->
println("onClick: " + numberPicker.value)
}
d.setNegativeButton("Cancel") { dialogInterface, i -> }
val alertDialog = d.create()
alertDialog.show()
}
and number_picker_dialog.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal">
<NumberPicker
android:id="@+id/dialog_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>