premkumarroyal / MonthAndYearPicker

The month and year picker for Android. Now you can pick month and year or only month or only year.
MIT License
162 stars 52 forks source link

How to get the OK and CANCEL event ? #18

Closed kalanidhidev closed 5 years ago

kalanidhidev commented 5 years ago

I am unable to get OK button event and cancel button event. I need to listen those button event do some action kindly help me ?

premkumarroyal commented 5 years ago

You can use OnDateSetListener for the OK action button. On Cancel button click dialog get dismissed and there is no public listener exposed.

MonthPickerDialog.Builder builder = new MonthPickerDialog.Builder(MainActivity.this, new MonthPickerDialog.OnDateSetListener() { @Override public void onDateSet(int selectedMonth, int selectedYear) { // on date set } }, today.get(Calendar.YEAR), today.get(Calendar.MONTH));

kalanidhidev commented 5 years ago

Got the answer and working fine


  val b = MonthPickerDialog.Builder(context,
                    MonthPickerDialog.OnDateSetListener { _, _ ->
                        // on date set }
                    }, today.get(Calendar.YEAR), today.get(Calendar.MONTH)

            )
                val view: View = b.datePicker
                val cancelButton = view.findViewById<TextView>(R.id.cancel_action)

                val okButton = view.findViewById<TextView>(R.id.ok_action)

                cancelButton.setOnClickListener {
                    b.dismiss()
                    birthYearTIET.text = null
                }
                okButton.setOnClickListener {
                    b.dismiss()
                    birthYearTIET.setText((Calendar.getInstance().get(Calendar.YEAR) - Constants.MIN_AGE).toString())
                }
nicemak commented 3 years ago

Got the answer and working fine


  val b = MonthPickerDialog.Builder(context,
                    MonthPickerDialog.OnDateSetListener { _, _ ->
                        // on date set }
                    }, today.get(Calendar.YEAR), today.get(Calendar.MONTH)

            )
                val view: View = b.datePicker
                val cancelButton = view.findViewById<TextView>(R.id.cancel_action)

                val okButton = view.findViewById<TextView>(R.id.ok_action)

                cancelButton.setOnClickListener {
                    b.dismiss()
                    birthYearTIET.text = null
                }
                okButton.setOnClickListener {
                    b.dismiss()
                    birthYearTIET.setText((Calendar.getInstance().get(Calendar.YEAR) - Constants.MIN_AGE).toString())
                }

Thanks