Site icon C1CTech

Android Jetpack Compose Example

&NewLine;<p class&equals;"has-text-align-left">This tutorial is about Android Jetpack compose and how to use it in an android application with the help of simple Book application&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;030d74&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidJetpackComposeExp" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"rXQIC7BPDQU" title&equals;"Android Jetpack Compose Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;rXQIC7BPDQU"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;rXQIC7BPDQU&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Jetpack Compose Example"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<h4 id&equals;"what-is-jetpack-compose"><strong><span style&equals;"color&colon; &num;000080&semi;">Jetpack Compose&quest;<&sol;span><&sol;strong><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>Jetpack Compose is a modern toolkit for building native Android UI&period; Jetpack Compose simplifies and accelerates UI development on Android with less code&comma; powerful tools&comma; and intuitive Kotlin APIs&period; <&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>To use Jetpack Compose you need to use at least a version &gt&semi;&equals; Android Studio Arctic Fox&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Composable Function<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>In Jetpack Compose&comma; Composable functions are used to define all the UI of your app programmatically&period; So&comma; you need not use any XML files for the layout of the app&period; All you need to make a composable function is just by using the <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Composable<&sol;span><&sol;strong> annotation to the function name&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>The basic syntax of a Composable function is&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&commat;Composable<&sol;span><&sol;strong><br &sol;>fun HelloWorld&lpar;&rpar; &lbrace;<br &sol;> Text&lpar;"Hello&comma; World&excl;"&comma; style &equals; TextStyle&lpar;color &equals; Color&period;Red&rpar;&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>To use <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorld&lpar;&rpar;<&sol;span><&sol;strong> composable function&comma; call it inside <span style&equals;"color&colon; &num;008000&semi;"><strong>setContent&lpar;&rpar;<&sol;strong><&sol;span> like this&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">setContent &lbrace;<br &sol;> HelloWorld&lpar;&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading" id&equals;"toc-anchor-009"><span style&equals;"color&colon; &num;000080&semi;"><strong>Previewing a Composable<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>Android Studio provides an awesome feature of previewing your UI components in the studio itself and that too dynamically&period; So&comma; whenever you want to test some UI components&comma; then you can simply preview it in the Android Studio by making a Composable function and by using the <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Preview<&sol;strong><&sol;span> annotation&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Composable<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Preview<&sol;strong><&sol;span><br &sol;>fun HelloWorld&lpar;&rpar; &lbrace;<br &sol;> Text&lpar;"Hello&comma; World&excl;"&comma; style &equals; TextStyle&lpar;color &equals; Color&period;Red&rpar;&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>After you import the annotation&comma; you’ll see the preview of the composable show up in the preview on the right-hand side of the screen&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-image"> &NewLine;<figure class&equals;"aligncenter"><img class&equals;"wp-image-2820" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;10&sol;output&period;png" alt&equals;"" &sol;><&sol;figure> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b class&equals;"graf--bold">NOTE&colon; You can&&num;8217&semi;t pass any parameter to a Preview Composable function&period;<&sol;b><&sol;span><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating BookApp<&sol;strong><&sol;span><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>Let&&num;8217&semi;s create a project which will contain two screens&comma; first screen will display list of items &lpar;each item will contain information about a book&rpar; and on click of each item will open a second screen which will display the information of the clicked item&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Gradle Dependencies<&sol;span><&sol;strong><&sol;h5> &NewLine; &NewLine; &NewLine; &NewLine;<p>Add this inside in the <span style&equals;"color&colon; &num;008000&semi;"><strong>android&lbrace;&rcub;<&sol;strong><&sol;span> block of your <strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">android&lbrace;<br &sol;> kotlinOptions &lbrace;<br &sol;> jvmTarget &equals; '1&period;8'<br &sol;> &rcub;<br &sol;> buildFeatures &lbrace;<br &sol;> compose true<br &sol;> &rcub;<br &sol;> composeOptions &lbrace;<br &sol;> kotlinCompilerExtensionVersion "1&period;0&period;3"<br &sol;> kotlinCompilerVersion '1&period;5&period;30'<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>Add the below Compose dependencies mainly used while working with jetpack compose&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">dependencies&lbrace;<br &sol;><br &sol;><span class&equals;"hljs-keyword">def<&sol;span> composeVersion &equals; <span class&equals;"hljs-string">"1&period;0&period;1"<br &sol;><br &sol;><&sol;span>implementation "androidx&period;compose&period;ui&colon;ui-tooling&colon;&dollar;compose&lowbar;version"<br &sol;>implementation "androidx&period;compose&period;foundation&colon;foundation&colon;&dollar;compose&lowbar;version"<br &sol;>implementation "androidx&period;compose&period;ui&colon;ui&colon;&dollar;compose&lowbar;version"<br &sol;>implementation "androidx&period;compose&period;material&colon;material&colon;&dollar;compose&lowbar;version"<br &sol;>implementation 'androidx&period;activity&colon;activity-compose&colon;1&period;3&period;0-alpha06'<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Code Implementation<&sol;span><&sol;strong><&sol;h5> &NewLine; &NewLine; &NewLine; &NewLine;<p>1&period; The <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;kt<&sol;span><&sol;strong> file represents the first screen which consist of a list&period;<&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;<&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;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;jetpackcomposeexp<br &sol;><br &sol;>import android&period;content&period;Context<br &sol;>import android&period;content&period;Intent<br &sol;>import android&period;os&period;Bundle<br &sol;>import android&period;os&period;Parcelable<br &sol;>import androidx&period;activity&period;ComponentActivity<br &sol;>import androidx&period;activity&period;compose&period;setContent<br &sol;>import androidx&period;annotation&period;DrawableRes<br &sol;>import androidx&period;compose&period;foundation&period;clickable<br &sol;>import androidx&period;compose&period;foundation&period;lazy&period;items<br &sol;>import androidx&period;compose&period;foundation&period;layout&period;&ast;<br &sol;>import androidx&period;compose&period;foundation&period;lazy&period;LazyColumn<br &sol;>import androidx&period;compose&period;foundation&period;shape&period;CornerSize<br &sol;>import androidx&period;compose&period;foundation&period;shape&period;RoundedCornerShape<br &sol;>import androidx&period;compose&period;material&period;&ast;<br &sol;>import androidx&period;compose&period;runtime&period;Composable<br &sol;>import androidx&period;compose&period;ui&period;Modifier<br &sol;>import androidx&period;compose&period;ui&period;draw&period;clip<br &sol;>import androidx&period;compose&period;ui&period;graphics&period;Color<br &sol;>import androidx&period;compose&period;ui&period;layout&period;ContentScale<br &sol;>import androidx&period;compose&period;ui&period;platform&period;LocalContext<br &sol;>import androidx&period;compose&period;ui&period;res&period;painterResource<br &sol;>import androidx&period;compose&period;ui&period;text&period;font&period;FontStyle<br &sol;>import androidx&period;compose&period;ui&period;text&period;font&period;FontWeight<br &sol;>import androidx&period;compose&period;ui&period;tooling&period;preview&period;Preview<br &sol;>import androidx&period;compose&period;ui&period;unit&period;dp<br &sol;>import androidx&period;core&period;content&period;ContextCompat&period;startActivity<br &sol;>import com&period;c1ctech&period;jetpackcomposeexp&period;ui&period;theme&period;JetpackComposeExpTheme<br &sol;>import kotlinx&period;android&period;parcel&period;Parcelize<br &sol;>import androidx&period;compose&period;foundation&period;Image as Image<br &sol;><br &sol;><br &sol;>val bookList&colon; List&lt&semi;Book&gt&semi; &equals; listOf&lpar;<br &sol;> Book&lpar;R&period;drawable&period;anny&lowbar;frank&comma; "The Diary of A Young Girl"&comma; "Anne Frank"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;alexander&lowbar;the&lowbar;great&comma; "Alexander the Great"&comma; "Jacob Abbott"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;autobiography&lowbar;of&lowbar;a&lowbar;yogi&comma; "Autobiography of a Yogi"&comma; "Paramahansa Yogananda"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;fluffy&lowbar;and&lowbar;me&comma; "Fluffy and Me"&comma; "Anita Krishan"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;my&lowbar;inventions&comma; "My Inventions"&comma; "Nikola Tesla"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;the&lowbar;enchanted&lowbar;castle&comma; "The Enchanted Castle"&comma; "E&period; Nesbit"&rpar;&comma;<br &sol;> Book&lpar;R&period;drawable&period;the&lowbar;secret&lowbar;garden&comma; "The Secret Garden"&comma; " Frances Hodgson Burnett"&rpar;<br &sol;>&rpar;<br &sol;><br &sol;><br &sol;>class MainActivity &colon; ComponentActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContent &lbrace;<br &sol;> JetpackComposeExpTheme &lbrace;<br &sol;> Column&lpar;modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&rpar; &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Material Design top app bar displays information<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; and actions relating to the current screen&period;<&sol;span><&sol;strong><br &sol;> TopAppBar&lpar;title &equals; &lbrace;<br &sol;> Text&lpar;"BookApp"&rpar;<br &sol;> &rcub;&rpar;<br &sol;> BookList&lpar;bookList&comma; this&commat;MainActivity&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating data class Book&comma; which implements Parcelable<&sol;span><&sol;strong><br &sol;>&commat;Parcelize<br &sol;>data class Book&lpar;<br &sol;> &commat;DrawableRes val imageResource&colon; Int&comma;<br &sol;> val bookTitle&colon; String&comma;<br &sol;> val bookAuthor&colon; String<br &sol;>&rpar; &colon; Parcelable<br &sol;><br &sol;>&commat;Composable<br &sol;>fun BookList&lpar;books&colon; List&lt&semi;Book&gt&semi;&comma; context&colon; Context&rpar; &lbrace;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;The vertically scrolling list that only composes<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; and lays out the currently visible items&period;<&sol;strong><&sol;span><br &sol;> LazyColumn&lpar;&rpar; &lbrace;<br &sol;> items&lpar;books&rpar; &lbrace; book -&gt&semi;<br &sol;> BookListItem&lpar;book&comma; context&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun BookListItem&lpar;book&colon; Book&comma; context&colon; Context&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;used to make a CardView&period;<&sol;span><&sol;strong><br &sol;> Card&lpar;<br &sol;> shape &equals; RoundedCornerShape&lpar;8&period;dp&rpar;&comma;<br &sol;> elevation &equals; 8&period;dp&comma;<br &sol;> modifier &equals; Modifier<br &sol;> &period;padding&lpar;16&period;dp&rpar;<br &sol;> &period;clickable &lbrace;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;passing book instance to DetailActivity<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;open DetailActivity on item click<&sol;strong><&sol;span><br &sol;> val intent &equals; Intent&lpar;context&comma; DetailActivity&colon;&colon;class&period;java&rpar;<br &sol;> intent&period;putExtra&lpar;"bookData"&comma; book&rpar;<br &sol;> startActivity&lpar;context&comma; intent&comma; null&rpar;<br &sol;><br &sol;> &rcub;<br &sol;> &rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;places its children in a horizontal sequence&period;<&sol;span><&sol;strong><br &sol;> Row&lpar;<br &sol;> modifier &equals; Modifier<br &sol;> &period;padding&lpar;8&period;dp&rpar;<br &sol;> &rpar; &lbrace;<br &sol;> BookImage&lpar;book&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;places its children in a vertical sequence&period;<&sol;span><&sol;strong><br &sol;> Column&lpar;<br &sol;> modifier &equals; Modifier<br &sol;> &period;weight&lpar;6f&comma; true&rpar;<br &sol;> &period;padding&lpar;20&period;dp&comma; 0&period;dp&comma; 0&period;dp&comma; 0&period;dp&rpar;&comma;<br &sol;> &rpar; &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;used to display text<&sol;strong><&sol;span><br &sol;> Text&lpar;<br &sol;> text &equals; book&period;bookTitle&comma;<br &sol;> style &equals; MaterialTheme&period;typography&period;subtitle1&comma;<br &sol;> color &equals; Color&period;Red<br &sol;> &rpar;<br &sol;> Text&lpar;<br &sol;> text &equals; "by &dollar;&lbrace;book&period;bookAuthor&rcub;"&comma;<br &sol;> style &equals; MaterialTheme&period;typography&period;subtitle2&comma;<br &sol;> fontStyle &equals; FontStyle&period;Italic&comma;<br &sol;> fontWeight &equals; FontWeight&period;Bold&comma;<br &sol;> color &equals; Color&period;Blue&comma;<br &sol;> &rpar;<br &sol;><br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun BookImage&lpar;book&colon; Book&quest;&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;used to display an image&period;<&sol;span><&sol;strong><br &sol;> Image&lpar;<br &sol;> painter &equals; painterResource&lpar;book&excl;&excl;&period;imageResource&rpar;&comma;<br &sol;> contentDescription &equals; null&comma;<br &sol;> contentScale &equals; ContentScale&period;FillBounds&comma;<br &sol;> modifier &equals; Modifier<br &sol;> &period;size&lpar;84&period;dp&rpar;<br &sol;> &period;clip&lpar;RoundedCornerShape&lpar;corner &equals; CornerSize&lpar;8&period;dp&rpar;&rpar;&rpar;<br &sol;> &rpar;<br &sol;>&rcub;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Preview can be applied to &commat;Composable methods with no parameters<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;to show them in the Android Studio preview&period;<&sol;strong><&sol;span><br &sol;>&commat;Preview<br &sol;>&commat;Composable<br &sol;>fun bookListPreview&lpar;&rpar; &lbrace;<br &sol;> JetpackComposeExpTheme &lbrace;<br &sol;> Column&lpar;modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&rpar; &lbrace;<br &sol;> TopAppBar&lpar;title &equals; &lbrace;<br &sol;> Text&lpar;"BookApp"&rpar;<br &sol;> &rcub;&rpar;<br &sol;> BookList&lpar;<br &sol;> bookList&comma;<br &sol;> LocalContext&period;current<br &sol;> &rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><strong>Column&colon; <&sol;strong><&sol;span>A Column is a layout composable which simply lays out all its child composables in a vertical column&period; It is similar to LinearLayout with vertical orientation&period; <&sol;li> &NewLine;<li class&equals;"graf--h2"><span style&equals;"color&colon; &num;000080&semi;"><strong>Lazy Column&colon; <&sol;strong><&sol;span>The vertically scrolling list that only composes and lays out the currently visible items&period;<&sol;li> &NewLine;<li class&equals;"graf--h2"><span style&equals;"color&colon; &num;000080&semi;"><b class&equals;"graf--bold">Card&colon; <&sol;b><&sol;span>A Card is a layout composable that is used to make a CardView&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><strong>Clickable&colon; <&sol;strong><&sol;span>makes a composable react with the user by using Clickable&period; Clickable supports a one-time click&comma; double click&comma; and long press&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><strong>Image&colon; <&sol;strong><&sol;span>To display an image&comma; we can use Image composable&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<p>2&period; The <strong><span style&equals;"color&colon; &num;008000&semi;">DetailActivity&period;kt<&sol;span><&sol;strong> file represents the second screen&comma; which will open on click of each item&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">DetailActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;jetpackcomposeexp<br &sol;><br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity<br &sol;>import android&period;os&period;Bundle<br &sol;>import androidx&period;activity&period;compose&period;setContent<br &sol;>import androidx&period;compose&period;foundation&period;Image<br &sol;>import androidx&period;compose&period;foundation&period;layout&period;&ast;<br &sol;>import androidx&period;compose&period;runtime&period;Composable<br &sol;>import androidx&period;compose&period;ui&period;Modifier<br &sol;>import androidx&period;compose&period;ui&period;layout&period;ContentScale<br &sol;>import androidx&period;compose&period;ui&period;res&period;painterResource<br &sol;>import androidx&period;compose&period;ui&period;unit&period;dp<br &sol;><br &sol;>class DetailActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContent &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;passing book instance from composable function<&sol;span><&sol;strong><br &sol;> BookImageFullScreen&lpar;intent&period;getParcelableExtra&lpar;"bookData"&rpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun BookImageFullScreen&lpar;book&colon; Book&quest;&rpar; &lbrace;<br &sol;> Image&lpar;<br &sol;> painter &equals; painterResource&lpar;book&excl;&excl;&period;imageResource&rpar;&comma;<br &sol;> contentDescription &equals; null&comma;<br &sol;> contentScale &equals; ContentScale&period;Crop&comma;<br &sol;> modifier &equals; Modifier<br &sol;> &period;fillMaxWidth&lpar;&rpar;<br &sol;> &period;fillMaxHeight&lpar;&rpar;<br &sol;> &period;padding&lpar;16&period;dp&rpar;<br &sol;> &rpar;<br &sol;>&rcub;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<figure class&equals;"wp-block-image"><img class&equals;"wp-image-2826" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;10&sol;Screenshot&lowbar;20211011-194150&lowbar;JetpackComposeExp-498x1024&period;jpg" alt&equals;"" width&equals;"315" height&equals;"648" &sol;>        <img class&equals;"alignnone wp-image-2827" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;10&sol;Screenshot&lowbar;20211011-194205&lowbar;JetpackComposeExp-498x1024&period;jpg" alt&equals;"" width&equals;"314" height&equals;"646" &sol;><&sol;figure> &NewLine;&NewLine;

Exit mobile version