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 [email protected] 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?
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With