'Custom pop-up numberPicker in Fragment Android

On a buttonpress I want to display a pop-up numberPicker. The button to be pressed is in a fragment. The code used for the pop-up numberPicker I found on the internet. The project i downloaded itself works perfectly fine but it also just has the MainActivity.kt an xml.

This is how the fragment looks. The one in the red circle is the button in question. --> https://i.stack.imgur.com/eZG9R.png

This is how the pop-up numberPicker looks. --> https://i.stack.imgur.com/bRcfR.png

The pop-up numberPicker has its own class and xml file. Now in the xml file there is an android:onClick="onOKButtonClick" but when i hover over it in the xml file it just says

Function "onOKButtonClick" is never used

fun onOKButtonClick() {} 

in the fragment code is grayed out.

Also when i try an onClickListener i can't find the "OK"button for the pop-up numberPicker. pop-up numberPicker button XML Code:

<Button
            android:id="@+id/button_common_alert_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/common_ok_button"
            android:onClick="onOKButtonClick"
            android:text="OK"
            android:textSize="15sp" />

I m able to close the pop-up numberPicker by pressing the OK button but I also want to set the text on the red circled button in my fragment to the selected number and that I don't know how to do. The code for the pop-up numberPicker:

class NumberPickerDialog {
private var mAlertDialog: AlertDialog? = null
private var mLoadView: View? = null
var mMax = 10
var mMin = 1
var mCurrent = 1
fun show(context: Context?) {
    mAlertDialog = AlertDialog.Builder(context!!).create()
    mLoadView = LayoutInflater.from(context).inflate(R.layout.dialog_number_picker, null)
    mAlertDialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    mAlertDialog!!.setView(mLoadView, 0, 0, 0, 0)
    mAlertDialog!!.setCanceledOnTouchOutside(true)
    val newMLoadView = mLoadView
    val btnOk = newMLoadView?.findViewById<Button>(R.id.button_common_alert_ok)
    btnOk?.setOnClickListener{
        dismiss()
    }
    val numPicker = newMLoadView?.findViewById<NumberPicker>(R.id.numberPicker)
    numPicker?.maxValue = mMax
    numPicker?.minValue = mMin
    numPicker?.value = mCurrent
    numPicker?.setOnValueChangedListener { _, _, newVal -> mCurrent = newVal }
    try {
        mAlertDialog!!.show()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun dismiss() {
    if (mAlertDialog != null) {
        mAlertDialog!!.dismiss()
    }
}

}

Here i close the pop-up when pressing ok:

btnOk?.setOnClickListener{
    dismiss()
}

Now if i could somehow gain access to the button in the fragment i could set the value of the text for this button at the same spot but i just don't know how to do it.

The code for the fragment:

class SecondFragment : Fragment() {
private lateinit var datePickerDialog: DatePickerDialog
private var numberPickerDialog = NumberPickerDialog()

private val sharedViewModel: ViewModel by activityViewModels()
private var _binding: FragmentSecondBinding? = null

private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    _binding = FragmentSecondBinding.inflate(inflater, container, false)

    sharedViewModel.imageView.observe(viewLifecycleOwner) { imageView ->
        binding.ivImage.setImageBitmap(imageView)
    }

    return binding.root

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding.datePickerButton.text = getTodaysDate()

    binding.numberPickerButton.setOnClickListener {
        numberPickerDialog.show(requireActivity())
    }

    binding.datePickerButton.setOnClickListener {
        initDatePicker()
        datePickerDialog.show()
    }
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

private fun getTodaysDate(): String { }
private fun initDatePicker() {}
private fun makeDateString(day: Int, month: Int, year: Int): String {}
private fun getMonthFormat(month: Int): String? { }

fun onOKButtonClick() {
    
}

}

So if anyone could help me out that would be awesome.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source