Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Last active December 12, 2018 10:54
Show Gist options
  • Save EmmanuelGuther/fc91617ca8dcf3975b358f95b8e15b8c to your computer and use it in GitHub Desktop.
Save EmmanuelGuther/fc91617ca8dcf3975b358f95b8e15b8c to your computer and use it in GitHub Desktop.
Template for custom dialog whit callback
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.timbrit.profesionales.R
import org.jetbrains.anko.sdk27.coroutines.onClick
class CustomDialog : DialogFragment() {
private lateinit var rootView: View
private lateinit var callback: CallbackCustomDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
rootView = inflater.inflate(R.layout.fragment_dialog, container, false)
callback = targetFragment as CallbackCustomDialog
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUp()
}
override fun onResume() {
super.onResume()
val window = dialog?.window
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
window?.setGravity(Gravity.CENTER)
}
private fun setUp() {
rootView.btnFoo.onClick { }
}
interface CallbackCustomDialog {
fun callbackFoo(foo: String?)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment