Site icon C1CTech

Android Working With Fragments

&NewLine;<p class&equals;"p1">This tutorial is about how to create a fragment and include it in an activity with the help of an example&period;<&sol;p> &NewLine; &NewLine; &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;AndroidFragmentDemo" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<h3 class&equals;"wp-block-heading" id&equals;"create"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"devsite-heading" role&equals;"heading" aria-level&equals;"2">Create a fragment class<&sol;span><&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">To create a fragment&comma; we have to define a fragment class&comma; which extends the class <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">Fragment<&sol;span><&sol;strong><&sol;span> and overrides the necessary methods&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted p1"><b>class <&sol;b>FragmentOne &colon; Fragment&lpar;&rpar; &lbrace; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">    <&sol;span><b>override fun <&sol;b>onCreateView&lpar; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">        <&sol;span>inflater&colon; LayoutInflater&comma; container&colon; ViewGroup&quest;&comma; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">        <&sol;span>savedInstanceState&colon; Bundle&quest; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>&rpar;&colon; View&quest; &lbrace; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">        <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;"><i>&sol;&sol; Inflate the layout for this fragment<&sol;i><&sol;span><&sol;strong> &NewLine; &NewLine;<i><span class&equals;"Apple-converted-space">        <&sol;span><&sol;i><b>return <&sol;b>inflater&period;inflate&lpar;R&period;layout&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;"><i>fragment&lowbar;one<&sol;i><&sol;span><&sol;strong>&comma; container&comma; <b>false<&sol;b>&rpar; &NewLine; &NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>FragmentOne<&sol;strong><&sol;span>&colon; subclass of Fragment&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1"><span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi; color&colon; &num;000000&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;one&colon;<&sol;strong><&sol;span> <&sol;span><span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;"> layout of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragmentOne&comma;<&sol;strong><&sol;span> which is defined in a separate XML file with the name<&sol;span><strong style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;"><span style&equals;"color&colon; &num;008000&semi;"> fragment&lowbar;one&period;xml&period;<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h3 class&equals;"wp-block-heading" id&equals;"add"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"devsite-heading" role&equals;"heading" aria-level&equals;"2">Add a fragment to an activity<&sol;span><&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">To add a fragment to any activity&comma; you can follow any of the following 2 approaches&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"ul1 wp-block-list"> &NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Add a fragment via XML<&sol;b><&sol;span><&sol;li> &NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Add a fragment programmatically<&sol;b><&sol;span><&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">In either case&comma; you need to add a <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;FragmentContainerView"><span class&equals;"s3">FragmentContainerView<&sol;span><&sol;a><&sol;span><&sol;strong> which extends the <strong><span style&equals;"color&colon; &num;008000&semi;">FrameLayout<&sol;span><&sol;strong> view group&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">It is strongly recommended to always use a <strong><span class&equals;"s4">FragmentContainerView<&sol;span><&sol;strong> as the container for fragments&comma; as <span class&equals;"s4">FragmentContainerView<&sol;span> includes fixes specific to fragments that other view groups such as <span class&equals;"s4">FrameLayout<&sol;span> do not provide&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Add a fragment via XML<&sol;b><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li class&equals;"p1">To add a fragment to your activity layout&&num;8217&semi;s XML&comma; use a <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s4">FragmentContainerView<&sol;span><&sol;strong><&sol;span> element&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;000000&semi;"><span class&equals;"s4">In <&sol;span><span class&equals;"s4">FragmentContainerView&comma;<&sol;span><&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s4"> android&colon; name<&sol;span><&sol;span> <&sol;strong>or<strong> <span style&equals;"color&colon; &num;0000ff&semi;">class <&sol;span><&sol;strong>attribute specifies the class name of the <span class&equals;"s4">Fragment<&sol;span> to instantiate&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted p1"><span class&equals;"s4">&lt&semi;androidx&period;fragment&period;app&period;FragmentContainerView<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;id&equals;"&commat;&plus;id&sol;fragment&lowbar;container&lowbar;view"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;name&equals;"com&period;example&period;ExampleFragment"<&sol;strong><&sol;span> &sol;&gt&semi;<&sol;span><&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">When the activity&&num;8217&semi;s layout is inflated&comma; the specified fragment is instantiated&comma; <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;onInflate&lpar;android&period;content&period;Context&comma;&percnt;2520android&period;util&period;AttributeSet&comma;&percnt;2520android&period;os&period;Bundle&rpar;"><span class&equals;"s3">onInflate&lpar;&rpar;<&sol;span><&sol;a> <&sol;span><&sol;strong>is called on the newly instantiated fragment&comma; and a <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s4">FragmentTransaction<&sol;span><&sol;strong><&sol;span> is created to add the fragment to the <span class&equals;"s4">FragmentManager<&sol;span>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h5 class&equals;"p4 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Implementation of Fragment via XML<&sol;b><&sol;span><&sol;h5> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">In this example&comma; we will see how to statically add two fragments to an activity&comma; by adding two <strong><span class&equals;"s4">FragmentContainerView<&sol;span><&sol;strong> elements directly in our app&&num;8217&semi;s main layout XML file&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong> defines the activity layout which consists of two <strong><span style&equals;"color&colon; &num;008000&semi;">FragmentContainerView<&sol;span><&sol;strong> elements with different <span style&equals;"color&colon; &num;008000&semi;"><strong>class<&sol;strong><&sol;span> attributes &lpar;<strong>FragmentOne and FragmentTwo<&sol;strong>&rpar;&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span> <&sol;strong><&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; &NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android" &NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; android&colon;orientation&equals;"vertical" &NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi; &NewLine; &NewLine; &lt&semi;androidx&period;fragment&period;app&period;FragmentContainerView &NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fcv&lowbar;one" &NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">class&equals;"com&period;c1ctech&period;androidfragmentdemo&period;FragmentOne"<&sol;span><&sol;strong> &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;weight&equals;"5" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;androidx&period;fragment&period;app&period;FragmentContainerView &NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fcv&lowbar;two" &NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">class&equals;"com&period;c1ctech&period;androidfragmentdemo&period;FragmentTwo"<&sol;span><&sol;strong> &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;weight&equals;"5" &sol;&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">fragment&lowbar;one&period;xml<&sol;span> <&sol;strong>defines the layout of the fragment with the name <span style&equals;"color&colon; &num;008000&semi;"><strong>FragmentOne<&sol;strong><&sol;span>&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;one&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; &NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android" &NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;light" &NewLine; tools&colon;context&equals;"&period;FragmentOne"&gt&semi; &NewLine; &NewLine; &lt&semi;TextView &NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content" &NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content" &NewLine; android&colon;layout&lowbar;centerInParent&equals;"true" &NewLine; android&colon;text&equals;"FragmentOne" &NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white" &NewLine; android&colon;textSize&equals;"30sp" &NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">FragmentOne<&sol;span><&sol;strong> defines the subclass of Fragment corresponding to the layout <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment&lowbar;one&period;xml&period;<&sol;strong> <&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>FragmentOne&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;androidfragmentdemo &NewLine;import android&period;os&period;Bundle &NewLine;import androidx&period;fragment&period;app&period;Fragment &NewLine;import android&period;view&period;LayoutInflater &NewLine;import android&period;view&period;View &NewLine;import android&period;view&period;ViewGroup &NewLine; &NewLine;class FragmentOne &colon; Fragment&lpar;&rpar; &lbrace; &NewLine; &NewLine; override fun onCreateView&lpar; &NewLine; inflater&colon; LayoutInflater&comma; container&colon; ViewGroup&quest;&comma; &NewLine; savedInstanceState&colon; Bundle&quest; &NewLine; &rpar;&colon; View&quest; &lbrace; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Inflate the layout for this fragment &NewLine;<&sol;strong><&sol;span> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;one&comma; container&comma; false&rpar; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>Similarly&comma; the layout XML file and the class for the second fragment &lpar; <strong>FragmentTwo <&sol;strong>&rpar; will be&colon;<&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;">fragment&lowbar;two&period;xml<&sol;span><&sol;strong><&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; &NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android" &NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;bright" &NewLine; tools&colon;context&equals;"&period;FragmentTwo"&gt&semi; &NewLine; &NewLine; &lt&semi;TextView &NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content" &NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content" &NewLine; android&colon;layout&lowbar;centerInParent&equals;"true" &NewLine; android&colon;text&equals;"FragmentTwo" &NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white" &NewLine; android&colon;textSize&equals;"30sp" &NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>FragmentTwo&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;androidfragmentdemo &NewLine; &NewLine;import android&period;os&period;Bundle &NewLine;import androidx&period;fragment&period;app&period;Fragment &NewLine;import android&period;view&period;LayoutInflater &NewLine;import android&period;view&period;View &NewLine;import android&period;view&period;ViewGroup &NewLine; &NewLine;class FragmentTwo &colon; Fragment&lpar;&rpar; &lbrace; &NewLine; &NewLine; override fun onCreateView&lpar; &NewLine; inflater&colon; LayoutInflater&comma; container&colon; ViewGroup&quest;&comma; &NewLine; savedInstanceState&colon; Bundle&quest; &NewLine; &rpar;&colon; View&quest; &lbrace; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Inflate the layout for this fragment &NewLine;<&sol;strong><&sol;span> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;two&comma; container&comma; false&rpar; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>There is no need to make any changes in the activity while adding the fragment via XML to an activity&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>When you run the app it will look like this&colon;<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<figure class&equals;"wp-block-image is-resized"> &NewLine;<figure id&equals;"attachment&lowbar;2601" aria-describedby&equals;"caption-attachment-2601" style&equals;"width&colon; 379px" class&equals;"wp-caption alignnone"><img class&equals;"wp-image-2601" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;04&sol;AndroidCreateFragmentViaXmlDemo&period;png" alt&equals;"" width&equals;"379" height&equals;"674" &sol;><figcaption id&equals;"caption-attachment-2601" class&equals;"wp-caption-text"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>An activity containing two fragments with different UI&period;<&sol;strong><&sol;span><&sol;figcaption><&sol;figure> &NewLine;<&sol;figure> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Add a fragment programmatically<&sol;b><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">To dynamically add a fragment to your activity&&num;8217&semi;s layout&comma; the layout should include a <strong><span class&equals;"s4" style&equals;"color&colon; &num;008000&semi;">FragmentContainerView<&sol;span><&sol;strong> to serve as a fragment container&comma; as shown in the following example&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted p1"><span class&equals;"s4">&lt&semi;androidx&period;fragment&period;app&period;FragmentContainerView<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;id&equals;"&commat;&plus;id&sol;fragment&lowbar;container&lowbar;view"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;span> &NewLine; &NewLine;<span class&equals;"s4">    android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &sol;&gt&semi;<&sol;span><&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li class&equals;"p1">Instead of using <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s4">android&colon;name <&sol;span><&sol;strong><span class&equals;"s4"><span style&equals;"color&colon; &num;000000&semi;">or<&sol;span><&sol;span><strong><span class&equals;"s4"> class<&sol;span><&sol;strong><&sol;span> attribute &lpar; instantiate the specific fragment automatically&rpar;&comma; a <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;FragmentTransaction"><span class&equals;"s3">FragmentTransaction<&sol;span><&sol;a><&sol;span><&sol;strong> is used to instantiate a fragment and add it to the activity&&num;8217&semi;s layout&period;<&sol;li> &NewLine;<li class&equals;"p1">While your activity is running&comma; you can make fragment transactions such as <span style&equals;"color&colon; &num;008000&semi;"><strong>adding<&sol;strong><&sol;span>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">removing<&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;008000&semi;">replacing<&sol;span><&sol;strong> a fragment&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<h5 class&equals;"p4 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Implementation of F<&sol;b><b>ragment programmatically<&sol;b><&sol;span><&sol;h5> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">In this example&comma; we will see how to dynamically add fragments to an activity on click of buttons&comma; by adding only one <strong><span class&equals;"s4">FragmentContainerView<&sol;span><&sol;strong> element in our app&&num;8217&semi;s main layout XML file&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">The <strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong> defines the activity layout which consists of a <strong><span style&equals;"color&colon; &num;008000&semi;">FragmentContainerView<&sol;span><&sol;strong> element and two buttons&comma; on click of which we will add different fragments in <strong><span style&equals;"color&colon; &num;008000&semi;">FragmentContainerView<&sol;span><&sol;strong> element at run time&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&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; &NewLine;&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" &NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto" &NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi; &NewLine; &NewLine; &lt&semi;Button &NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentOne" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content" &NewLine; android&colon;text&equals;"FragmentOne" &NewLine; android&colon;textAllCaps&equals;"false" &NewLine; android&colon;textSize&equals;"20sp" &NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;light" &NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent" &NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;Button &NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentTwo" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content" &NewLine; android&colon;layout&lowbar;marginTop&equals;"5dp" &NewLine; android&colon;text&equals;"FragmentTwo" &NewLine; android&colon;textAllCaps&equals;"false" &NewLine; android&colon;textSize&equals;"20sp" &NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;bright" &NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentOne" &NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentOne" &NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentOne" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;androidx&period;fragment&period;app&period;FragmentContainerView &NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fragment&lowbar;container&lowbar;view" &NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent" &NewLine; android&colon;layout&lowbar;height&equals;"0dp" &NewLine; android&colon;layout&lowbar;marginTop&equals;"5dp" &NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent" &NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent" &NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;fragmentTwo" &sol;&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>The code for the main activity class <span style&equals;"color&colon; &num;008000&semi;"><b>MainActivity&period;kt<&sol;b><&sol;span> will be as follows&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>MainActivity&period;kt<&sol;b><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;androidfragmentdemo &NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity &NewLine;import android&period;os&period;Bundle &NewLine;import androidx&period;fragment&period;app&period;add &NewLine;import androidx&period;fragment&period;app&period;commit &NewLine;import kotlinx&period;android&period;synthetic&period;main&period;activity&lowbar;main&period;&ast; &NewLine; &NewLine;class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace; &NewLine; override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace; &NewLine; super&period;onCreate&lpar;savedInstanceState&rpar; &NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar; &NewLine; &NewLine; btn&lowbar;fragmentOne&period;setOnClickListener &lbrace; &NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> supportFragmentManager&period;commit &lbrace; &NewLine; add&lt&semi;FragmentOne&gt&semi;&lpar;R&period;id&period;fragment&lowbar;container&lowbar;view&rpar;<&sol;span><&sol;strong> &NewLine; &rcub; &NewLine; &rcub; &NewLine; &NewLine; btn&lowbar;fragmentTwo&period;setOnClickListener &lbrace; &NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> supportFragmentManager&period;commit &lbrace; &NewLine; add&lt&semi;FragmentTwo&gt&semi;&lpar;R&period;id&period;fragment&lowbar;container&lowbar;view&rpar;<&sol;span><&sol;strong> &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">In the above code&comma; to add the fragment dynamically to the activity&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li class&equals;"p1">The <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;FragmentManager"><span class&equals;"s1">FragmentManager<&sol;span><&sol;a><&sol;span><&sol;strong> is used to create a <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s2">FragmentTransaction<&sol;span><&sol;strong><&sol;span>&period;<&sol;li> &NewLine;<li class&equals;"p1">Then&comma; to instantiate the fragment <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;fragment&sol;app&sol;FragmentTransaction&num;add&lpar;int&comma;&percnt;20java&period;lang&period;Class&percnt;3C&quest;&percnt;20extends&percnt;20androidx&period;fragment&period;app&period;Fragment&percnt;3E&comma;&percnt;20android&period;os&period;Bundle&rpar;"><span class&equals;"s1">FragmentTransaction&period;add&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span> is used&comma; passing in the <span class&equals;"s2">ViewGroup<&sol;span> ID of the container in the layout and the fragment class we want to add and then commited the transaction&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>When you run the app it will look like this&colon;<&sol;strong><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<figure class&equals;"wp-block-image is-resized"> &NewLine;<figure id&equals;"attachment&lowbar;2649" aria-describedby&equals;"caption-attachment-2649" style&equals;"width&colon; 337px" class&equals;"wp-caption alignleft"><img class&equals;"wp-image-2649" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1625131945&period;png" alt&equals;"" width&equals;"337" height&equals;"599" &sol;><figcaption id&equals;"caption-attachment-2649" class&equals;"wp-caption-text"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Button FragmentOne is clicked<&sol;strong><&sol;span><&sol;figcaption><&sol;figure> &NewLine;<figure id&equals;"attachment&lowbar;2650" aria-describedby&equals;"caption-attachment-2650" style&equals;"width&colon; 336px" class&equals;"wp-caption alignleft"><img class&equals;"wp-image-2650" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1625131941&period;png" alt&equals;"" width&equals;"336" height&equals;"597" &sol;><figcaption id&equals;"caption-attachment-2650" class&equals;"wp-caption-text"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Button FragmentTwo is clicked<&sol;strong><&sol;span><&sol;figcaption><&sol;figure> &NewLine;<&sol;figure> &NewLine; &NewLine; &NewLine;&NewLine;

Exit mobile version