Site icon C1CTech

Android DialogFragment Example

<p>This article is about Android DialogFragment and how to work with it with the help of simple examples&period;<&sol;p> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Android DialogFragment<&sol;span> <&sol;strong><&sol;h4> &NewLine;<p>A <strong><span style&equals;"color&colon; &num;008000&semi;">Dialog Fragment<&sol;span> <&sol;strong>is a fragment that is used to make Dialogs that floats on Activity&period;<&sol;p> &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;520599&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidDialogFragmentExample" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"> <&sol;h4> &NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"uz3TBr7HxsM" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;uz3TBr7HxsM" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;uz3TBr7HxsM&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;uz3TBr7HxsM&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Android DialogFragment<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>A <span style&equals;"color&colon; &num;008000&semi;"><strong>DialogFragment <&sol;strong><&sol;span>is a fragment that is used to make Dialogs that floats on Activity&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>DialogFragment extends the <span style&equals;"color&colon; &num;008000&semi;"><strong>Fragment<&sol;strong><&sol;span> class&period; All the information regarding the Dialog or the data associated with the Dialog will be stored or managed in the Fragment only&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>Since DialogFragment is associated with Fragment&comma; so it has its own LifeCycle and now the Activity need not manage the lifecycle of Dialogs&period; Due to this reason&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>DialogFragments are recommended to be used while implementing Alert Dialog or any other type of Dialog in Android&period;<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"p1 wp-block-heading"><span class&equals;"s1" style&equals;"color&colon; &num;000080&semi;"><b>DialogFragment lifecycle<&sol;b><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p2">A <span class&equals;"s1">DialogFragment<&sol;span> follows the standard fragment lifecycle&period; In addition&comma; <span class&equals;"s1">DialogFragment<&sol;span> has a few additional lifecycle callbacks&period; The most common ones are as follows&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"ul1 wp-block-list"> &NewLine;<li class&equals;"li2"><span class&equals;"s2"><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;DialogFragment&num;onCreateDialog&lpar;android&period;os&period;Bundle&rpar;"><span class&equals;"s3"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onCreateDialog&lpar;&rpar;<&sol;strong><&sol;span><&sol;span><&sol;a><&sol;span> &&num;8211&semi; Override this callback to provide a <span class&equals;"s1">Dialog<&sol;span> for the fragment to manage and display&period;<&sol;li> &NewLine;<li class&equals;"li2"><strong><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;DialogFragment&num;onDismiss&lpar;android&period;content&period;DialogInterface&rpar;"><span class&equals;"s3">onDismiss&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> &&num;8211&semi; Override this callback if you need to perform any custom logic when your <span class&equals;"s1">Dialog<&sol;span> is dismissed&comma; such as releasing resources&comma; unsubscribing from observable resources&comma; and so on&period;<&sol;li> &NewLine;<li class&equals;"li2"><strong><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;DialogFragment&num;onCancel&lpar;android&period;content&period;DialogInterface&rpar;"><span class&equals;"s3">onCancel&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> &&num;8211&semi; Override this callback if you need to perform any custom logic when your <span class&equals;"s1">Dialog<&sol;span> is cancelled&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p2"><span class&equals;"s1">DialogFragment<&sol;span> also contains methods to dismiss or set the cancellability of your <span class&equals;"s1">DialogFragment<&sol;span>&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"ul1 wp-block-list"> &NewLine;<li class&equals;"li2"><strong><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;DialogFragment&num;dismiss&lpar;&rpar;"><span class&equals;"s3">dismiss&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> &&num;8211&semi; Dismiss the fragment and its dialog&period; <&sol;li> &NewLine;<li class&equals;"li2"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s2"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;DialogFragment&num;setCancelable&lpar;boolean&rpar;"><span class&equals;"s3">setCancellable&lpar;&rpar;<&sol;span><&sol;a><&sol;span> <&sol;span><&sol;strong>&&num;8211&semi; Control whether the shown <span class&equals;"s1">Dialog<&sol;span> is cancelable&period; This method should be used instead of directly calling <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Dialog&num;setCancelable&lpar;boolean&rpar;"><span class&equals;"s4">Dialog&period;setCancelable&lpar;boolean&rpar;<&sol;span><&sol;a>&period;<&sol;span><&sol;strong><&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Steps of creating custom Dialog<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>To create a dialog fragment of our own follows the below steps&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li>Create a Kotlin&sol;Java file for your Dialog Fragment&period; For example&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyDialog&period;kt<&sol;strong><&sol;span> and this class will extend the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>DialogFragment<&sol;strong><&sol;span>&period; Here in this class&comma; override all the methods related to dialog&period;<&sol;li> &NewLine;<li>Create the layout file of the dialog&period; <&sol;li> &NewLine;<li>Call your custom dialog from your Activity&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>1&period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><b>File <&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;"> New Android Project<&sol;span>&comma;<&sol;b> select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span>&comma; provide <span style&equals;"color&colon; &num;008000&semi;"><strong>app name<&sol;strong>&comma;<&sol;span> select language to <strong><span style&equals;"color&colon; &num;008000&semi;">kotlin<&sol;span> <&sol;strong>and then finally click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>2&period; Open app-level <strong><span style&equals;"color&colon; &num;008000&semi;">build&period;gradle<&sol;span><&sol;strong> file and add the dependency of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Livedata<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>ViewModel<&sol;strong><&sol;span>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">dependencies &lbrace;<br &sol;><span style&equals;"color&colon; &num;0000ff&semi;"><strong> &sol;&sol;viewmodel and livedata dependency<&sol;strong><&sol;span><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> implementation "androidx&period;lifecycle&colon;lifecycle-viewmodel-ktx&colon;2&period;2&period;0"<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> implementation "androidx&period;lifecycle&colon;lifecycle-livedata-ktx&colon;2&period;2&period;0"<&sol;span><&sol;strong><br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Custom Dialog Example<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>1 &period; Create a layout file named <strong><span style&equals;"color&colon; &num;008000&semi;">fragment&lowbar;custom&lowbar;dialog&period;xm<&sol;span>l<&sol;strong> which represents the UI of the custom dialog&period; It consists of a title&comma; a subtitle&comma; and two buttons &lpar;for positive and negative responses&rpar;&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;custom&lowbar;dialog&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;padding&equals;"12dp"&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatTextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tvTitle"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;textSize&equals;"24sp"<br &sol;> android&colon;textColor&equals;"&commat;android&colon;color&sol;black"<br &sol;> android&colon;textStyle&equals;"bold"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatTextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tvSubTitle"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"12dp"<br &sol;> android&colon;textColor&equals;"&commat;android&colon;color&sol;black"<br &sol;> android&colon;textSize&equals;"20sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;id&sol;tvTitle" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnPositive"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"16dp"<br &sol;> android&colon;text&equals;"OK"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tvSubTitle" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnNegative"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"16dp"<br &sol;> android&colon;text&equals;"CANCEL"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toStartOf&equals;"&commat;id&sol;btnPositive"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tvSubTitle" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>2&period; Create a <strong><span style&equals;"color&colon; &num;008000&semi;">CustomDialogFragment<&sol;span><&sol;strong> class &lpar;subclass of DialogFragment&rpar; under the root directory&comma; <strong>right-click &gt&semi; New &gt&semi; Kotlin File&sol;Class<&sol;strong>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>CustomDialogFragment&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;dialogfragmentdemo<br &sol;><br &sol;>import android&period;os&period;Bundle<br &sol;>import android&period;view&period;LayoutInflater<br &sol;>import android&period;view&period;View<br &sol;>import android&period;view&period;ViewGroup<br &sol;>import android&period;view&period;WindowManager<br &sol;>import androidx&period;fragment&period;app&period;DialogFragment<br &sol;>import kotlinx&period;android&period;synthetic&period;main&period;fragment&lowbar;custom&lowbar;dialog&period;view&period;&ast;<br &sol;><br &sol;>class CustomDialogFragment &colon; DialogFragment&lpar;&rpar; &lbrace;<br &sol;><br &sol;> companion object &lbrace;<br &sol;><br &sol;> const val TAG &equals; "CustomDialogFragment"<br &sol;><br &sol;> private const val KEY&lowbar;TITLE &equals; "KEY&lowbar;TITLE"<br &sol;> private const val KEY&lowbar;SUBTITLE &equals; "KEY&lowbar;SUBTITLE"<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;take the title and subtitle form the Activity<&sol;strong><&sol;span><br &sol;> fun newInstance&lpar;title&colon; String&comma; subTitle&colon; String&rpar;&colon; CustomDialogFragment &lbrace;<br &sol;> val args &equals; Bundle&lpar;&rpar;<br &sol;> args&period;putString&lpar;KEY&lowbar;TITLE&comma; title&rpar;<br &sol;> args&period;putString&lpar;KEY&lowbar;SUBTITLE&comma; subTitle&rpar;<br &sol;> val fragment &equals; CustomDialogFragment&lpar;&rpar;<br &sol;> fragment&period;arguments &equals; args<br &sol;> return fragment<br &sol;> &rcub;<br &sol;><br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating the Dialog Fragment&period;<&sol;span><&sol;strong><br &sol;> override fun onCreateView&lpar;<br &sol;> inflater&colon; LayoutInflater&comma;<br &sol;> container&colon; ViewGroup&quest;&comma;<br &sol;> savedInstanceState&colon; Bundle&quest;<br &sol;> &rpar;&colon; View&quest; &lbrace;<br &sol;> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;custom&lowbar;dialog&comma; container&comma; false&rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;tasks that need to be done after the creation of Dialog<&sol;span><&sol;strong><br &sol;> override fun onViewCreated&lpar;view&colon; View&comma; savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onViewCreated&lpar;view&comma; savedInstanceState&rpar;<br &sol;> setupView&lpar;view&rpar;<br &sol;> setupClickListeners&lpar;view&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onStart&lpar;&rpar; &lbrace;<br &sol;> super&period;onStart&lpar;&rpar;<br &sol;> dialog&quest;&period;window&quest;&period;setLayout&lpar;<br &sol;> WindowManager&period;LayoutParams&period;MATCH&lowbar;PARENT&comma;<br &sol;> WindowManager&period;LayoutParams&period;WRAP&lowbar;CONTENT<br &sol;> &rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;setting the text in CustomDialog<&sol;span><&sol;strong><br &sol;> private fun setupView&lpar;view&colon; View&rpar; &lbrace;<br &sol;><br &sol;> view&period;tvTitle&period;text &equals; arguments&quest;&period;getString&lpar;KEY&lowbar;TITLE&rpar;<br &sol;> view&period;tvSubTitle&period;text &equals; arguments&quest;&period;getString&lpar;KEY&lowbar;SUBTITLE&rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;setting all the click listeners of the CustomDialog<&sol;span><&sol;strong><br &sol;> private fun setupClickListeners&lpar;view&colon; View&rpar; &lbrace;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; On clicking the positive&sol;negative button&comma;<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; the dialog will be closed with the help of dismiss&lpar;&rpar;<&sol;strong><&sol;span><br &sol;> view&period;btnPositive&period;setOnClickListener &lbrace;<br &sol;> dismiss&lpar;&rpar;<br &sol;> &rcub;<br &sol;> view&period;btnNegative&period;setOnClickListener &lbrace;<br &sol;> dismiss&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>3&period; Now&comma; from the MainActivity&comma; we will open the custom Dialog on button click&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">btnCustomDialog&period;setOnClickListener &lbrace;<br &sol;> CustomDialogFragment&period;newInstance&lpar;<br &sol;> getString&lpar;R&period;string&period;custom&lowbar;dialog&lowbar;title&rpar;&comma;<br &sol;> getString&lpar;R&period;string&period;custom&lowbar;dialog&lowbar;subTitle&rpar;<br &sol;> &rpar;&period;show&lpar;supportFragmentManager&comma; CustomDialogFragment&period;TAG&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>Run the app&period; On click of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Custom Dialog Fragment<&sol;strong><&sol;span> button the custom dialog will appear like this&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><img class&equals;"alignnone wp-image-2685" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626279519-576x1024&period;png" alt&equals;"" width&equals;"386" height&equals;"687" &sol;><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"> <&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating an Alert Dialog Example<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">If you want to use the AlertDialog provided by Android instead of your own custom dialog&comma; then you have to override the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>onCreateDialog<&sol;strong><&sol;span> method and create your AlertDialog there&period; <&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Note&colon;<&sol;span><&sol;strong> No need to override <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;Fragment&num;oncreateview"><span class&equals;"s4">onCreateView&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;Fragment&num;onViewCreated&lpar;android&period;view&period;View&comma;&percnt;20android&period;os&period;Bundle&rpar;"><span class&equals;"s4">onViewCreated&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> when using a <span class&equals;"s1">DialogFragment<&sol;span> with a <span class&equals;"s1">Dialog&lpar;provided by Android&rpar;<&sol;span>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>1&period; Create the AlertDialogFragment class <span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;under the root directory&comma; right-click &gt&semi; New &gt&semi; Kotlin File&sol;Class&rpar;<&sol;strong><&sol;span>&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AlertDialogFragment&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;dialogfragmentdemo<br &sol;><br &sol;>import android&period;app&period;Dialog<br &sol;>import android&period;content&period;DialogInterface<br &sol;>import android&period;os&period;Bundle<br &sol;>import androidx&period;appcompat&period;app&period;AlertDialog<br &sol;>import androidx&period;fragment&period;app&period;DialogFragment<br &sol;><br &sol;>class AlertDialogFragment &colon; DialogFragment&lpar;&rpar; &lbrace;<br &sol;><br &sol;> companion object &lbrace;<br &sol;> const val TAG &equals; "AlertDialogFragment"<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;create fragment instance<&sol;span><&sol;strong><br &sol;> fun newInstance&lpar;&rpar;&colon; AlertDialogFragment &lbrace;<br &sol;> val fragment &equals; AlertDialogFragment&lpar;&rpar;<br &sol;> return fragment<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating the AlertDialog<&sol;span><&sol;strong><br &sol;> override fun onCreateDialog&lpar;savedInstanceState&colon; Bundle&quest;&rpar;&colon; Dialog &lbrace;<br &sol;> val builder &equals; activity&quest;&period;let &lbrace; AlertDialog&period;Builder&lpar;it&rpar; &rcub;<br &sol;> builder&quest;&period;setTitle&lpar;R&period;string&period;alert&lowbar;dialog&lowbar;title&rpar;<br &sol;> builder&quest;&period;setMessage&lpar;R&period;string&period;alert&lowbar;dialog&lowbar;subTitle&rpar;<br &sol;> builder&quest;&period;setPositiveButton&lpar;"Yes"&comma; object &colon; DialogInterface&period;OnClickListener &lbrace;<br &sol;> override fun onClick&lpar;dialog&colon; DialogInterface&comma; which&colon; Int&rpar; &lbrace;<br &sol;> dismiss&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> builder&quest;&period;setNegativeButton&lpar;"No"&comma; object &colon; DialogInterface&period;OnClickListener &lbrace;<br &sol;> override fun onClick&lpar;dialog&colon; DialogInterface&comma; which&colon; Int&rpar; &lbrace;<br &sol;> dismiss&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> return builder&quest;&period;create&lpar;&rpar;&excl;&excl;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>Run the app&period; On click of  <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Alert Dialog Fragment<&sol;strong><&sol;span> button the Alertdialog will appear on the screen like this&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><img class&equals;"alignnone wp-image-2686" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626279513-576x1024&period;png" alt&equals;"" width&equals;"372" height&equals;"662" &sol;><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"> <&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Dialog containing data shared with Activity&sol;Fragment Example<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">In this example&comma; we have one EditText in the DialogFragment and a button&period; On clicking the button&comma; the text in the EditText will be displayed on the MainActivity&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>1&period; The layout file <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment&lowbar;dialog&lowbar;with&lowbar;data&period;xml<&sol;strong><&sol;span> represents the UI of the dialog&period; It consists of one TextView&comma; one EditText&comma; and a Button&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;dialog&lowbar;with&lowbar;data&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;padding&equals;"12dp"&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatTextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;title"<br &sol;> android&colon;layout&lowbar;width&equals;"0dp"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"Enter your details"<br &sol;> android&colon;textAlignment&equals;"center"<br &sol;> android&colon;textSize&equals;"24sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatEditText<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;etName"<br &sol;> android&colon;layout&lowbar;width&equals;"0dp"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"12dp"<br &sol;> android&colon;hint&equals;"Enter your name"<br &sol;> android&colon;textSize&equals;"16sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;title" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnSubmit"<br &sol;> android&colon;layout&lowbar;width&equals;"0dp"<br &sol;> android&colon;layout&lowbar;height&equals;"48dp"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"16dp"<br &sol;> android&colon;text&equals;"Submit"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;etName" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">NOTE&colon; Don&&num;8217&semi;t forget to add the dependency of LiveData and ViewModel&period;<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>2&period; In the <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong> file&comma; we need to add one button and one text view for displaying the name&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong> <&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnAlertDialog"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"48dp"<br &sol;> android&colon;layout&lowbar;margin&equals;"12dp"<br &sol;> android&colon;text&equals;"&commat;string&sol;btn&lowbar;AlertDialog"<br &sol;> android&colon;textAllCaps&equals;"false"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnCustomDialog"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"48dp"<br &sol;> android&colon;layout&lowbar;margin&equals;"12dp"<br &sol;> android&colon;text&equals;"&commat;string&sol;btn&lowbar;Custom&lowbar;Dialog"<br &sol;> android&colon;textAllCaps&equals;"false"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btnAlertDialog"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btnAlertDialog"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btnAlertDialog" &sol;&gt&semi;<br &sol;><br &sol;> <strong>&lt&semi;androidx&period;appcompat&period;widget&period;AppCompatButton<&sol;strong><br &sol;><strong> android&colon;id&equals;"&commat;&plus;id&sol;btnDataDialog"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;height&equals;"48dp"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;margin&equals;"12dp"<&sol;strong><br &sol;><strong> android&colon;text&equals;"&commat;string&sol;btn&lowbar;Dialog&lowbar;with&lowbar;data"<&sol;strong><br &sol;><strong> android&colon;textAllCaps&equals;"false"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btnCustomDialog"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btnCustomDialog"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btnCustomDialog" &sol;&gt&semi;<&sol;strong><br &sol;><br &sol;><strong> &lt&semi;androidx&period;appcompat&period;widget&period;AppCompatTextView<&sol;strong><br &sol;><strong> android&colon;id&equals;"&commat;&plus;id&sol;tvName"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong><br &sol;><strong> android&colon;layout&lowbar;margin&equals;"12dp"<&sol;strong><br &sol;><strong> android&colon;textSize&equals;"18sp"<&sol;strong><br &sol;><strong> android&colon;textStyle&equals;"bold"<&sol;strong><br &sol;><strong> android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;dark"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btnDataDialog"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btnDataDialog"<&sol;strong><br &sol;><strong> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btnDataDialog" &sol;&gt&semi;<&sol;strong><br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">3&period; Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>SharedViewModel<&sol;strong><&sol;span> and add the below code&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>SharedViewModel&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">class SharedViewModel &colon; ViewModel&lpar;&rpar; &lbrace;<br &sol;> val name &equals; MutableLiveData&lt&semi;String&gt&semi;&lpar;&rpar;<br &sol;><br &sol;> fun sendName&lpar;text&colon; String&rpar; &lbrace;<br &sol;> name&period;value &equals; text<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">4&period; Create a class named <strong><span style&equals;"color&colon; &num;008000&semi;">DialogWithDataFragment<&sol;span><&sol;strong> and add the below code&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>DialogWithDataFragment&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;dialogfragmentdemo<br &sol;><br &sol;>import android&period;os&period;Bundle<br &sol;>import android&period;view&period;LayoutInflater<br &sol;>import android&period;view&period;View<br &sol;>import android&period;view&period;ViewGroup<br &sol;>import android&period;view&period;WindowManager<br &sol;>import androidx&period;fragment&period;app&period;DialogFragment<br &sol;>import androidx&period;lifecycle&period;ViewModelProvider<br &sol;>import kotlinx&period;android&period;synthetic&period;main&period;fragment&lowbar;dialog&lowbar;with&lowbar;data&period;view&period;&ast;<br &sol;><br &sol;>class DialogWithDataFragment &colon; DialogFragment&lpar;&rpar; &lbrace;<br &sol;><br &sol;> companion object &lbrace;<br &sol;> const val TAG &equals; "DialogWithDataFragment"<br &sol;> &rcub;<br &sol;><br &sol;> private lateinit var viewModel&colon; SharedViewModel<br &sol;><br &sol;> override fun onCreateView&lpar;<br &sol;> inflater&colon; LayoutInflater&comma;<br &sol;> container&colon; ViewGroup&quest;&comma;<br &sol;> savedInstanceState&colon; Bundle&quest;<br &sol;> &rpar;&colon; View&quest; &lbrace;<br &sol;> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;dialog&lowbar;with&lowbar;data&comma; container&comma; false&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onViewCreated&lpar;view&colon; View&comma; savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onViewCreated&lpar;view&comma; savedInstanceState&rpar;<br &sol;> viewModel &equals; ViewModelProvider&lpar;requireActivity&lpar;&rpar;&rpar;&period;get&lpar;SharedViewModel&colon;&colon;class&period;java&rpar;<br &sol;> setupClickListeners&lpar;view&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onStart&lpar;&rpar; &lbrace;<br &sol;> super&period;onStart&lpar;&rpar;<br &sol;> dialog&quest;&period;window&quest;&period;setLayout&lpar;<br &sol;> WindowManager&period;LayoutParams&period;MATCH&lowbar;PARENT&comma;<br &sol;> WindowManager&period;LayoutParams&period;WRAP&lowbar;CONTENT<br &sol;> &rpar;<br &sol;> &rcub;<br &sol;><br &sol;> private fun setupClickListeners&lpar;view&colon; View&rpar; &lbrace;<br &sol;> view&period;btnSubmit&period;setOnClickListener &lbrace;<br &sol;> viewModel&period;sendName&lpar;view&period;etName&period;text&period;toString&lpar;&rpar;&rpar;<br &sol;> dismiss&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"graf--p">Here&comma; on click of the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>submit<&sol;strong><&sol;span> button&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong>sendName&lpar;&rpar; <&sol;strong><&sol;span>method of the <span style&equals;"color&colon; &num;008000&semi;"><strong>SharedViewModel<&sol;strong><&sol;span> is called&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"graf--p">Now&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong>name<&sol;strong><&sol;span> needs to be observed in the <strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity<&sol;span><&sol;strong> and also we need to implement the click listener of the button to open the <strong><span style&equals;"color&colon; &num;008000&semi;">DialogWithDataFragment<&sol;span><&sol;strong>&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>5&period; Given below is the complete code of <strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;dialogfragmentdemo<br &sol;><br &sol;>import android&period;os&period;Bundle<br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity<br &sol;>import androidx&period;lifecycle&period;Observer<br &sol;>import androidx&period;lifecycle&period;ViewModelProvider<br &sol;>import kotlinx&period;android&period;synthetic&period;main&period;activity&lowbar;main&period;&ast;<br &sol;><br &sol;>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> private lateinit var sharedViewModel&colon; SharedViewModel<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;<br &sol;><br &sol;> <strong> sharedViewModel &equals; ViewModelProvider&lpar;this&rpar;&period;get&lpar;SharedViewModel&colon;&colon;class&period;java&rpar;<&sol;strong><br &sol;><strong> sharedViewModel&period;name&period;observe&lpar;this&comma; Observer &lbrace;<&sol;strong><br &sol;><strong> tvName&period;text &equals; it<&sol;strong><br &sol;><strong> &rcub;&rpar;<&sol;strong><br &sol;><br &sol;> btnAlertDialog&period;setOnClickListener &lbrace;<br &sol;> AlertDialogFragment&period;newInstance&lpar;&rpar;&period;show&lpar;supportFragmentManager&comma; AlertDialogFragment&period;TAG&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> btnCustomDialog&period;setOnClickListener &lbrace;<br &sol;> CustomDialogFragment&period;newInstance&lpar;<br &sol;> getString&lpar;R&period;string&period;custom&lowbar;dialog&lowbar;title&rpar;&comma;<br &sol;> getString&lpar;R&period;string&period;custom&lowbar;dialog&lowbar;subTitle&rpar;<br &sol;> &rpar;&period;show&lpar;supportFragmentManager&comma; CustomDialogFragment&period;TAG&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> <strong>btnDataDialog&period;setOnClickListener &lbrace;<&sol;strong><br &sol;><strong> DialogWithDataFragment&lpar;&rpar;&period;show&lpar;supportFragmentManager&comma; DialogWithDataFragment&period;TAG&rpar;<&sol;strong><br &sol;><strong> &rcub;<&sol;strong><br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>Run the app and click on <span style&equals;"color&colon; &num;008000&semi;"><strong>Dialog with data Fragment <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">button&period; In the dialog&comma; <&sol;span><&sol;span>enter some data in the EditText&comma; click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>submit<&sol;strong><&sol;span> and see the text in MainActivity&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2687" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626279569-576x1024&period;png" alt&equals;"" width&equals;"348" height&equals;"618" &sol;>      <img class&equals;"alignnone wp-image-2688" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626279576-576x1024&period;png" alt&equals;"" width&equals;"351" height&equals;"624" &sol;>&NewLine;

Exit mobile version