Android Kotlin DialogFragment (TimePicker)

Viewed 10556

I'm having problems showing a DialogFragment with Android in Kotlin. This is what I got so far:

The TimePickerFragment.kt

package de.flokol120.UniversalSleepTimer

import android.app.Dialog
import android.app.DialogFragment
import android.widget.TimePicker
import android.app.TimePickerDialog
import android.os.Bundle
import android.text.format.DateFormat
import java.util.*


/**
 * @author flokol120
 * TimePicker Fragment to get the time
 */
class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)

        return TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()))
    }

    override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
        //TODO add this
    }
}

and the AddTimer.kt

package de.flokol120.UniversalSleepTimer

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button


/**
 * @author flokol120
 * the fragment which adds a simple kill Timer
 */
class AddTimer : Fragment() {

    lateinit var timePicker : Button

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val v = inflater!!.inflate(R.layout.fragment_add_timer, container, false)

        timePicker = v.findViewById(R.id.timePicker)

        timePicker.setOnClickListener {
            val newFragment = TimePickerFragment()
            newFragment.show(fragmentManager, "timePicker")
        }

        return v
    }

}

I also tried replacing the fragmentManager with this@AddTimer.fragmentManager but this doesn't seem to be the solution.

I am pretty clueless now because Android Studio says I have to use an FragmentManager! as first parameter but I am using one, right?

4 Answers

Aliaksandr Kavalenka came up with a neat workaround, which was for a date picker. I formatted his idea to work with a time picker:

fun getTime(textView: TextView, context: Context){

    val cal = Calendar.getInstance()

    val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
        cal.set(Calendar.HOUR_OF_DAY, hour)
        cal.set(Calendar.MINUTE, minute)

        textView.text = SimpleDateFormat("HH:mm").format(cal.time)
    }

    textView.setOnClickListener {
        TimePickerDialog(context, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true).show()
    }
}

Maybe that helps you

I have a function getDate()

fun getDate(textView: TextView, context: Context){

        val cal = Calendar.getInstance()

        val dateSetListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            cal.set(Calendar.YEAR, year)
            cal.set(Calendar.MONTH, monthOfYear)
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)

            textView.text = SimpleDateFormat("dd.MM.yyyy").format(cal.time)

        }

        textView.setOnClickListener {
            DatePickerDialog(context, dateSetListener,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH)).show()
        }
    }

If an Activity need call DatePickerDialog - I simply use my function

presenter.getDate(textViewTime, this)

presenter - it is object where is my function getDate

Looks like you are mixing Regular DialogFragment with Support Lib Fragment. You should extend "android.support.v4.app.DialogFragment" instead of "import android.app.DialogFragment".

Support lib uses support library version FragmentManager not regular Fragment Manager.

Hope this helps.

 private var date: DatePickerDialog.OnDateSetListener? = null

 date = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->

            myCalendar?.set(Calendar.YEAR, year)
            myCalendar?.set(Calendar.MONTH, monthOfYear)
            myCalendar?.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            val myFormat = "yyyy-MM-dd"
            val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
            Log.d("date_of_birth", sdf.format(myCalendar?.time))

        }
Related