Kotlin Synthetic And Custom Layout In DialogFragment
Solution 2:
It looks like this isn't supported by default yet, but I've found the easiest way to do it to be like this. In a base dialog class:
protected abstract val containerView: View
override fun getView() = containerView
In a subclass:
override val containerView by unsafeLazy {
View.inflate(context, R.layout.dialog_team_details, null) as ViewGroup
}
Then you can use the synthetic views as you normally would and use the containerView
as the view for your dialog.
Solution 3:
Previous answer will not work, because onViewCreated is not called when you use onCreateDialog. You should first import kotlinx...department_chooser_dialog.view.dep_list, an then use it as follows:
import kotlinx.android.synthetic.main.department_chooser_dialog.view.dep_list
...
class DepartmentChoiceDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val dialog = inflater.inflate(R.layout.department_chooser_dialog, null)
dialog.dep_list.layoutManager = LinearLayoutManager(activity)
dialog.dep_list.itemAnimator = DefaultItemAnimator()
dialog.dep_list.setHasFixedSize(true)
builder.setTitle(R.string.choose_or_create_dep)
.setView(dialog)
...
Solution 4:
Change to onCreateView implementation
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.department_chooser_dialog, container, false)
}
and use a custom title(TextView) and cancel(Button) in the department_chooser_dialog
onActivityCreated will run after onCreateView and will be just fine.
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dep_list.layoutManager = LinearLayoutManager(activity)
dep_list.itemAnimator = DefaultItemAnimator()
dep_list.setHasFixedSize(true)
}
Solution 5:
So I'm not sure if this has been solved... I just came across this. If you have a custom Dialog view make a class that extends DialogFragment and use the "dialog" object to import views in the layout. I'm using Android Studio 3.1.3
and Kotlin version 1.2.41
at the time of writing.
import kotlinx.android.synthetic.main.your_custom_layout.*
class SelectCountryBottomSheet : BottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setContentView(R.layout.your_custom_layout)
dialog.some_custom_close_button.setOnClickListener { dismiss() }
return dialog
}
}
Post a Comment for "Kotlin Synthetic And Custom Layout In DialogFragment"