Created
June 19, 2021 13:30
-
-
Save SCOTT-HAMILTON/22d52bd43bf15b14832d90919b7515a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credits go to https://stackoverflow.com/a/61340341 | |
package com.sample.piview | |
import android.content.Context | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.NumberPicker | |
import androidx.core.os.bundleOf | |
import androidx.fragment.app.setFragmentResult | |
import androidx.preference.PreferenceDialogFragmentCompat | |
class NumberPickerPreferenceDialog : PreferenceDialogFragmentCompat() { | |
lateinit var numberPicker: NumberPicker | |
override fun onCreateDialogView(context: Context?): View { | |
numberPicker = NumberPicker(context) | |
numberPicker.minValue = NumberPickerPreference.MIN_VALUE | |
numberPicker.maxValue = NumberPickerPreference.MAX_VALUE | |
return numberPicker | |
} | |
override fun onBindDialogView(view: View?) { | |
super.onBindDialogView(view) | |
numberPicker.value = (preference as NumberPickerPreference).getPersistedInt() | |
} | |
override fun onDialogClosed(positiveResult: Boolean) { | |
if (positiveResult) { | |
numberPicker.clearFocus() | |
val newValue: Int = numberPicker.value | |
parentFragmentManager.setFragmentResult("requestKey", | |
bundleOf("result" to newValue)) | |
} | |
} | |
companion object { | |
fun newInstance(key: String): NumberPickerPreferenceDialog { | |
val fragment = NumberPickerPreferenceDialog() | |
val bundle = Bundle(1) | |
bundle.putString(ARG_KEY, key) | |
fragment.arguments = bundle | |
return fragment | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment