When using a DialogFragment is not really convenient to create one. We even create a simple DialogFragment that we need to write many codes to archive it!
Therefore, a builder for creatingDialogFragment is bron. Creating a DialogFragment is just like using a builder for it. There're few advantages as the followings,
- Don't need to write the request code from activity for catching the event callback.
- Only few parameters, you can get a beautiful fragment dialog.
- DialogFragment Builder also supports the databinding in
MVVM
's architecture.
Also the complete code is here.
QuickDialogFragment.Builder(this) {
btnNegativeText = "negative" to { d -> /* What you want to do! */ }
btnPositiveText = "positive" to { d -> /* What you want to do! */ }
message = "The is message!"
title = "This is title!"
cancelable = false
}.build()
QuickDialogFragment.Builder(this) {
viewResCustom = R.layout.fragment_dialog_test
fetchComponents = { v, df ->
v.btn.setOnClickListener { /* What you want to do! */ }
v.tv.text = "I was clicked!"
}
}.build()
In the MVVM
architecture, you can create a dialog fragment as the following.
QuickDialogBindingFragment.Builder<FragmentDialogBindBinding>(this) {
viewCustom = R.layout.fragment_dialog_bind
}.build().apply {
bind = {
it.vm = TestActivityViewModel()
}
}.show()
The snippet reference sample. And layout xml file is here.
There're some issues I don't know how to fix it. If someone understands, please let me know kindly. 😀
In the some situations, you need to modify the size of the fragment dialog. You can do it as like this way.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ↓↓↓↓ Make double layout to assign your layout size. ↓↓↓↓ -->
<LinearLayout
android:layout_width="250dp"
android:layout_height="300dp"
android:orientation="vertical">
...
</LinearLayout>
</LinearLayout>
First you have to make sure your project bundle.gradle
as below:
allprojects {
repositories {
jcenter()
}
}
And add our dependency to your app bundle.gradle
.
implementation 'com.devrapid.jieyi:dialogbuilder:1.0.2'
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>dialogbuilder</artifactId>
<version>1.0.2</version>
<type>pom</type>
</dependency>
-[x] Change the dialog size