Android jetpack compose basics for text, image and list
This post will demonstrate how to create basic text view, image view and list view with Jetpack compose. The data object to be used is very simple, just a data class with 2 string properties. Message.kt
data class Message(val author: String, val body: String)
Here is a sample data with Messages, SampleData.kt
object SampleData { // Sample conversation data val conversationSample = listOf( Message( "Colleague", "Test...Test...Test..." ), Message( "Colleague", "List of Android versions:\n" + "Android KitKat (API 19)\n" + "Android Lollipop (API 21)\n" + "Android Marshmallow (API 23)\n" + "Android Nougat (API 24)\n" + "Android Oreo (API 26)\n" + "Android Pie (API 28)\n" + "Android 10 (API 29)\n" + "Android 11 (API 30)\n" + "Android 12 (API 31)\n" ), Message( "Colleague", "I think Kotlin is my favorite programming language.\n" + "It's so much fun!" ), Message( "Colleague", "Searching for alternatives to XML layouts..." ), Message( "Colleague", "Hey, take a look at Jetpack Compose, it's great!\n" + "It's the Android's modern toolkit for building native UI." + "It simplifies and accelerates UI development on Android." + "Less code, powerful tools, and intuitive Kotlin APIs :)" ), Message( "Colleague", "It's available from API 21+ :)" ), Message( "Colleague", "Writing Kotlin for UI seems so natural, Compose where have you been all my life?" ), Message( "Colleague", "Android Studio next version's name is Arctic Fox" ), Message( "Colleague", "Android Studio Arctic Fox tooling for Compose is top notch ^_^" ), Message( "Colleague", "I didn't know you can now run the emulator directly from Android Studio" ), Message( "Colleague", "Compose Previews are great to check quickly how a composable layout looks like" ), Message( "Colleague", "Previews are also interactive after enabling the experimental setting" ), Message( "Colleague", "Have you tried writing build.gradle with KTS?" ), ) }
MessageCard.kt, this is an example using composable to create a view group which contains text views and image view.
import android.content.res.Configuration import androidx.compose.foundation.Image import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @Composable fun MessageCard(msg: Message) { Row(modifier = Modifier.padding(all = 8.dp)) { Image( painter = painterResource(R.drawable.ic_baseline_account_circle_24), contentDescription = null, modifier = Modifier .size(40.dp) .clip(CircleShape) .border(1.5.dp, MaterialTheme.colors.secondaryVariant, CircleShape) ) Spacer(modifier = Modifier.width(8.dp)) // We keep track if the message is expanded or not in this // variable var isExpanded by remember { mutableStateOf(false) } // We toggle the isExpanded variable when we click on this Column Column(modifier = Modifier.clickable { isExpanded = !isExpanded }) { Text( text = msg.author, color = MaterialTheme.colors.secondaryVariant, style = MaterialTheme.typography.subtitle2 ) Spacer(modifier = Modifier.height(4.dp)) Surface( shape = MaterialTheme.shapes.medium, elevation = 1.dp, ) { Text( text = msg.body, modifier = Modifier.padding(all = 4.dp), // If the message is expanded, we display all its content // otherwise we only display the first line maxLines = if (isExpanded) Int.MAX_VALUE else 1, style = MaterialTheme.typography.body2 ) } } } } @Preview(name = "Light Mode") @Preview( uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true, name = "Dark Mode" ) @Composable fun PreviewMessageCard() { MessageCard( msg = Message("Colleague", "Hey, take a look at Jetpack Compose, it's great!") ) }
MainActivity.kt, this an example of using another composable inside a composable, creating a list and setting the content of an activity with a composable.
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.ui.tooling.preview.Preview import androidx.compose.foundation.lazy.items import androidx.compose.runtime.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Conversation(SampleData.conversationSample) } } } @Composable fun Conversation(messages: List) { LazyColumn { items(messages) { message -> MessageCard(message) } } } @Preview @Composable fun PreviewConversation() { Conversation(SampleData.conversationSample) }
dependencies:
ext { compose_version = '1.1.1' } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1' implementation 'androidx.activity:activity-compose:1.4.0' implementation 'com.google.android.material:material:1.6.0-alpha03' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" }
themes.xml
References:
https://developer.android.com/jetpack/compose/tutorial
https://github.com/codexpedia/android_basic_composable
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts