Building Android Applications with Jetpack Compose
Peculiar C. Umeh1:03:472023
Compose ersetzt die RecyclerView-Adapter-Routine durch eine einzige Composable-Funktion
Eine Liste unter Android anzuzeigen bedeutete früher ein XML-Layout, eine RecyclerView und eine separate Adapter-Klasse, die jede View erstellt und bindet. Android-Entwicklerin Peculiar C. Umeh baut live in Android Studio eine funktionierende Kurslisten-App und stößt dabei auf echte Gradle- und State-Fehler. Für alle, die noch XML-Layouts schreiben, stellt sich die Frage, wie viel dieses Aufwands eine einzige Kotlin-Datei tatsächlich ersetzen kann.
Sehen Sie, wie eine Compose-Listen-App live entsteht, von Composables und Modifiern bis zu State und LazyColumn
Android-Entwicklerin Peculiar C. Umeh programmiert live eine einfache Kurslisten-App und zeigt, wie Jetpack Compose native Android-UI in Kotlin mit weniger Code erstellt. Sie behandelt Composable-Funktionen, Previews, Modifier, Layouts, State und LazyColumn und argumentiert, dass Compose einfacher und schneller ist als der Ansatz mit XML und RecyclerView.
“Mit Jetpack Compose können Sie mithilfe eines deklarativen Programmiermodells schöne, responsive, flexible Benutzeroberflächen erstellen.”
“Das LazyColumn- oder LazyRow-Layout erstellt und bindet die View nach Bedarf.”
What you will learn
Ein Android Studio-Projekt aus der Vorlage „Empty Compose Activity“ mit Kotlin 1.5 oder neuer aufsetzen.
Composable-Funktionen schreiben und sie mit der Preview-Annotation sofort prüfen.
Modifier sowie die Layouts Column und Row nutzen, um UI-Elemente zu gestalten und anzuordnen.
TextField-Eingaben mit remember und Mutable State verwalten, Listen mit einer Mutable State List.
Eine RecyclerView samt Adapter durch eine LazyColumn aus Card-Composables ersetzen.
Details
Level
Beginner
Format
Tech talk
Technical depth
Practitioner
For
Student, Early career, Career changer
Length
1:03:47
Published
June 2023
Questions this talk answers
Was brauchen Sie, um eine App mit Jetpack Compose zu entwickeln?
Ein aktuelles Android Studio (die Referentin nutzt Flamingo) mit Kotlin 1.5 oder neuer sowie ein neues Projekt aus der Vorlage „Empty Compose Activity“, die die nötigen Abhängigkeiten und ein Beispiel-Composable hinzufügt.
Was ist eine Composable-Funktion?
Es ist eine als Composable annotierte Kotlin-Funktion, die ein UI-Element erzeugt, und der grundlegende Baustein einer Compose-UI.
Wie funktionieren Modifier in Jetpack Compose?
Modifier sind Funktionen, die mit dem Punktoperator auf einem Composable-Element aufgerufen werden, um dessen Aussehen oder Verhalten zu ändern, etwa Padding, Größe oder das Ausfüllen des Bildschirms.
Wie verfolgt ein TextField, was der Nutzer eingibt?
Ein TextField erhält einen Wert und ein onValueChange-Lambda; der Wert liegt in einem mit remember und Mutable State erzeugten State, und das Lambda aktualisiert ihn bei jeder neuen Eingabe.
Wie schneidet LazyColumn im Vergleich zu RecyclerView bei Listen ab?
RecyclerView benötigt ein XML-Layout und eine separate Adapter-Klasse, während LazyColumn in einem Composable definiert wird und Views bei Bedarf erstellt und bindet. Die Referentin hält Compose für einfacher, schneller und effizienter, wobei die Performance von der Komplexität der UI und der Anzahl der Elemente abhängt.
Warum LazyColumn den RecyclerView-Adapter schlägt (90 s)
Speaker: Good day, good afternoon, or good morning, wherever you're joining me on this session. My name is Beculia Umeh, and I'm glad to have you join me for this session today. Okay, I'm going to be picking up my slide so we can get straight to the topic I have today. Like I said, my name is Sikulia Umeh.
I am an Android engineer. And today in this session, I want to give shout out to our sponsors and women tech organizers for this opportunity. I'll be taking you through the topic building Android application with Jetpack Compose. Last year, I looked at - building Android application. Last two years, we looked at building Android application with, with comparing Java and Kotlin. And this year, we'll have a new awesome topic for you. And that is building Android application with Jetpack Compose. Okay, I'm going to dive deep to find out and to tell you a lot about Jetpack Compose. Okay. Now, Jetpack Compose is Speaker 1: Yeah, more than that is new and is modern toolkit for building native Android UIs. With Jetpack Compose, you can build beautiful, responsive, flexible user interface using a declarative programming model. Okay. In this session, I will
Speaker 1: I will guide you through the process of building an Android application using Jetpack Compose. My session will always be more of practical. Okay. So I'm going to guide you through how to develop Android application with Jetpack Compose. Jetpack Compose makes it developing Android application more easier, simple, and is awesome. So, Speaker 1: Speaker 1: we're all wasting time. I'm going to show you what we'll be working on in this session. So, Speaker 1: grab the next one. In this practical session, we're going to be building just a simple application just to show you how to use Jetpack Compose to build Android applications in a more concise, easy, and using just simple, intuitive centers. Okay. I'm going to stop sharing this while I will go over to Android Studio to create
Speaker 1: a new project and we'll talk about Jetpack Compose in full. Speaker 1: Okay. The next coming up will be my Android Studio. Speaker 1: In this Android Studio, for you to be able to develop or build an application using Jetpack Compose, the prerequisite side of what you need is an Android Studio. And you need Android Studio Erratic Force or Speaker 1: The latest Android Studio Erratic Force or the latest Android Studio.
Speaker 1: But I presently I'm using Flamingo. Then you can, for your Kotlin, you're going to have Kotlin 1.5 or the latest. So once you open the Android Studio, you need to just to get to new projects. And the new project, you're going to use an empty Jetpack Compose template, which is already here for you in Android Studio. The essence is for you to Speaker 1: to be able to have the dependence for you to get all the dependency necessary for you to work and build your application with ease. So I'm going to click name. Now, like the sample of application we will be working on, like I showed you, is just using Jetpack Compose to show Speaker 1: a list of courses that maybe programming languages or just courses that you want to learn for a certain period of time. So over here in the Android Studio, I'm going to just give this a name. Let me say learn a course.
Speaker 1: So I'm going to click finish. Speaker 1: I think that something happened. I'm going to share that again in my Android Studio. So we give Android Studio, we give it some time to build, to come up. Why Speaker 1: We'll continue with Speaker 1: We give Android Studio just a few times. It will come up complete with adding the dependency and the rest of them and get the platform environment ready for us. So like I said, for you to start working on your using Jetpack Compose, you need to do what? Create and use an empty Compose activity table. So you need to do what?
Speaker 1: Create and use an empty Compose activity template. The empty Compose activity template. Like I said, will create a project with necessary dependency and basic composable function that displays hello world. Hello Android, not hello world, but this time is hello Android. Okay. I mentioned composable function. In Jetpack Compose, we need to do what? Speaker 1: You need to do what?
Speaker 1: So we need to do what we need to do. Speaker 1: We need to do what we need to do what we need to do, and then we need to do. like I said, will create a project with necessary dependency and basic composable function that displays hello world, hello Android, not hello world, but this time it's hello Android.
Okay, I mentioned composable function. In Jetpack Compose, we use most composable function. What are composable functions? Composable functions are building blocks of Jetpack Compose UI. A composable function is a Kotlin function that returns what a UI element, okay? The composable function are notated at Compose annotation. In SML, when we use SML for developing Android application, we don't use annotation just when you want to build, write a function.
But when you are using like maybe dependency injection or something, you can add maybe some annotations, some other things you want to use. But just to build a simple UI that doesn't need much information on a screen, you might not really need to annotate anything except you don't want to go into like you is adding a room database and all the rest.
So our project is ready. Let's look at what we have here in Compose. Compose has made everything just so easy. It has created a simple, just simple project for us. Now let me show you one thing about Compose that it updates, it has a live update. So it updates whatever you're working on or any code you are adding to your application, it's updated live.
And that is what we are going to see when we use preview. Now, what's a preview to see exactly what we have here on this? Oh, I got that, yes, it's a normal thing and it's saying that this happened and fail application Java, like a griddle, Java is part of Android Studio. So all you need to do, just come over here and SDK location, you get to griddle setup and you change this to 17 Java.
Okay, let me use 17 and you just hit okay and okay. So rebuild this again, everything would be all right at this point. Yeah, we'll give it a few minutes to come up. So we're going to see a composable function that is displayed on this sample we have over here, let me pick it up again, yes, over here.
Now, this is a composable function, a composable function as you can see is annotated with what's composable and the name of this function is greeting. This greeting has a parameter name and then a modifier, which is a custom modifier. I will talk about that as we move along, as we move on in this section, and then it has what's a composable what's elements in it.
So this is an example of a composable function, just like I said earlier that in compose you have live updates. So what we have, we're going to happen is that I would like to see what I'm working on or what is exactly all the code I'm having. Now my UI is going to look like in a real life device.
So I'm going to just reflect this for me to be able to do that. I'm going to add preview. This preview will help me see how my DUI will look like in the real life device. OK, so I'm going to use the preview over here, and I'm also going to use the real life device so you can be able to see what you're working on.
OK. So we wait for that to come up while we look at and you don't need this. I don't need at this point and I don't need this now. I will add them later when I need them. So it's coming up. So over here, you've seen that this is called a composable function. And I made mention of something I talked about.
This composable function has a parameter. The parameter. The parameter is what that this part that the greatness takes is a function that takes in a name parameter and also a modifier, too, and then returns a test like you can see a a test UI that displays a greeting message. If you want to change how this test, this text you are seeing here look like or appear or does how your UI look like, you use what we call modifier.
So let me tell you about modifier. What modifier? Modifiers are used to do what to change the appearance or the behavior of a composable element. Modifiers are functions that are called on a composable element using the .operator. All you just need to do is add .operator, okay? Maybe operator, maybe padding or the size. You want to change the size of that composable element.
You can use . maybe front . . . . You can add . . . I mean, the size, the UI, the arrangement in the UI, how the UI will look like. You can use the fixed mass size or something. So let's look at what we have here already. Okay. Like, exactly what I have here. If I want to, if I don't want to use this modifier over here, okay, I can actually remove it from here and then I will use it on the body of . . .
of the test to modify . . . to modify this test. Okay. Now, like I mentioned, the preview has shown us. This is just what I have here, hello world. That's what the preview has shown us. Forget about this, because we may not need it . . . If I also run this on the physical device, which I'm going to do at this moment, you will see that on this, you will have just hello, Android.
That's only what you have. But for us to move ahead in this, like I said, what we are working on is just a simple application that accepts . . . That shows displays list of . . . That's what we're going to be working on. So I don't need this . . . We move ahead to . . .
Start . . . Building our project. Okay. So I have . . . I want to work on a just simple list. Like if you look at what I showed . . . The other slide I showed . . . The sample of . . . It has a test. It has a . . . A test field.
And . . . A button. The test field you see there . . . In SMA, it's called . . . Edit test. Okay. But over here in . . . Compose, it's called . . . A test field. So what I need to do is this . . . I have to have this at the back of my mind . . .
that I will need to use the best practice by creating . . . A package called . . . A presentation package. Presentation. So inside this package, I'm going to decide on what I'll be working . . . The name for the app I want to build. So I'm just going to create a file. And let's . . .
I think I used that . . . Okay. Yes. Let's say . . . Let me call it . . . Learn . . . Course. Okay. This is just . . . Like what I explained over there . . . We have . . . We had . . . The . . . A function that . . .
Android Studio created . . . Stateforwards. But now we're going to create this function on our own. So I'm going to start by having . . . Okay. I like this way . . . Compose. Then I get . . . At compose added. Like I explained earlier . . . That . . . It's . . .
A composable function is annotated with . . . At compose. So let me call this function . . . Learn . . . Course. Okay. Yes. Now, just like we saw greeting. Greeting was a function we saw over there. And inside greeting, we had . . . We had . . . We had a composable element . . .
What was a test. So in this . . . We were going to be creating composable elements here. So I'm going to start by creating one. I'm looking at the UI. Don't forget. I've already shown us the UI of what we're going to be working on. At the end . . . What we're achieving at the end of this.
So I'm going to create composable elements called test. Okay. So this is . . . It will take a parameter. It will take string. And then I can call it . . Let's say . . . List . . . Of . . . Courses. Okay. Now, if I preview here, I will not be able to see anything.
If I try, I will not be able to see anything. You know why? The reason is because there's no preview. I didn't add anything about at preview over here. So nothing will happen. So what I need for me to see on the Android . . . Or have this update for me. Have the live update. I'm just going to have a review.
A preview. For this function. Okay. For this to be able to work, I'm going to annotate this with at preview. At preview. So, for this to be able to work, I'm going to annotate this with at preview. You see it's updating immediately. So, then I'm going to call this function. Over here. So, that I will see what I have worked with.
Okay. So, for this to be able to work, I'm going to annotate this with at preview. At preview. You see it's updating immediately. So, then I'm going to call this function over here. So, that I will see what I have worked on. Learn. Of course. Automatically, this is going to preview. So, I'm going to annotate this with at preview.
At preview. Okay. You see it's updating immediately. So, then I'm going to call this function over here. So, that I will see what I have worked on. Learn. Of course. Automatically, this is going to preview. Automatically, this is going to preview for us to see what we have achieved at this point. I will give you a few minutes for that to happen.
Exactly. You can look at my Android Studio. You're going to see list of courses. That's the composable element that I just added. Okay. So, we'll move on to. Remember, I made mention of modifier. Modifier. And I said that we can use modifier to do what? They are used to change the appearance or the behavior of a composable element.
And I said that modifiers are functions that are called on a composable element using .operator. So, over here, I'm going to call user modifier to make... And another thing about Compose is once you click on this, you bring your cursor close to this test field. You can see all the parameters that this test can accept. We can increase the size, the color.
You see the modifier. You can use this modifier to add the padding and the rest of them on the test. So, let's just add, increase the size. We're going to use font, font size, because I saw you can accept, yes, font size. Let's make the font size like 17.dp. It's not here. In Kotlin, you just add dp close to this, but here, you add .dp.
Let's see why we have this. Okay. Okay, we'll call that again. I'm going to come back to that again. All right. I'm going to add that again. It's not there. Okay. We'll allow that to build. All right. That's done. So, let me use modifier here. Let's add modifier. We'll add modifier. Oh, that's showing the modifier. I'll use this one.
Modifier. I'll use this for the UI. Let's add padding. Like I said, the padding, let's say the padding is, let me add padding top. The padding top should be, of course, let's say, distinct.dp. Okay. So, this is what we've been able to achieve using the modifier. Like we talk about composable function and modifier. So, we're going to move ahead for us to have a better way.
The better way for us to arrange our UI in a more organized way is to use layouts. Okay. What is a layout or what are layouts? Layouts are used to arrange composable elements on the screen. There are several inbuilt layouts in Jetpack Compose. We have column. We have row. We have both. And then there is constraint layout.
Okay. And this is what we are trying to achieve to build a list of courses. We're going to use column. Like if you go back again to the sample of what we are trying to achieve or the app we're building, you will see on that sample, I have a test. We have added a test. And I have a test field.
And then I have a button. So, now for me to be able to have all this in one place, I'm going to use a column. That is one of the layouts I made mention. Let me pull that out and then add a column. I have column. Okay. And then the next one I will add. I will add back the test.
So, in this column, because I have the next thing I need to add is what I call a test field. It's better for me not to have everything jump back together. So, I can use a spacer. I need that spacer. Then add modifier to modifier. So, I can have space in between the. Okay. I use modifier. Padding.
Let me use padding. Let me use padding of, let's say, HTTP. Awesome. So, the next I have on the sample I showed us earlier is a test field and a button by the side. Okay. So, how do I achieve that? That just on the same line, the horizontal arrangement. So, I can use, like column is used to arrange things vertically.
Why? I can use row to arrange, I mean, arrange composable function vertically. I can use row to arrange composable function on a horizontal manner. So, the next thing I'm going to do is to add a test field. I prefer using outline, maybe outline test field and outline button. So, I'm going to grab my outline, outline test field.
Okay. And okay. So, over here, I'm seeing some, I have errors. What does that mean? If you look at the error I have there, you see that the value is showing that error, meaning that that value needs something. Let's go back to XML where you have a detest. A detest accepts input. The same way with test field.
Test field accepts input. So, the value is referring to the input that this test field is expecting to get. Now, the next one you can see there is on value change. Now, what does that mean? On value change is called whenever there is a new input in the test field. That's when the on value change is called.
The on value change. Okay. Lander returns a new value that you can use to update the test field state. I'm going to say that again. I said that this on value change you are seeing there. On value change. Lander returns what? A new value that you can use to do or to update the test field state. I just mentioned state.
So, I'm going to say a little bit about state before we can move on. Now, state in an app is what? It's any value that can change over time. Okay. It's any value at all that can change over time in your application. That is the state is just a very broad definition. This is a very broad definition.
It compares everything from maybe a room database to a variable in a class. I can say that the state is used to do what? To store or update the state of a composable element. State is declared using the remember function, which creates a mutable state object that can update using the set update as the set state function.
So, we're going to go back. I'm going to show you how this has been done on the Android schedule. So, over here, like I mentioned, I'm going to create a value for this, a variable for this value. And I'm going to call that val, let me say val, test, test value. This test value will be equal to, remember, I said something about remember that we used state.
So, I'm going to use state at this point. I can say by remember this. So, I will use mutable state of. Mutable state of. And this is going to have an empty string because I see errors here. We're not afraid of these errors. Because we see them. Let me change this value. Let's find out what this error is saying.
We're talking about extension. Okay, that's, we don't need that. First, let's use a course tool. Then the error goes away. So, I'll then, on this value, I'll call the variable that I've just created here, over here. Now, I'm still having error here. So, over here, I can then add color brackets. I said, okay, new. New. Test. Okay, test.
Value. Then, like I showed you about the state and the lander will be equals to this test view. Test value. Then, equals to the new. The new value. Okay. Okay, let's see. The argument that that's showing. We're having a few errors that we're going to fix. Without wasting time. Okay, let me see what's. Going on why we have.
The error over there. Aha. Okay, let me change the variables for this to be. By. And then. Because it's a list. Is a new table state. Import that. Get that extension, which is not exactly what we need. At this point. Metable states value. We can change this to. Metable states off. Okay. Mutable states. That's why we're having the.
The error. This is not right. We chose the wrong thing. This is mutable states. Off. And then we have. Okay. I'm going to. Import that. Then check it again. and import that. And all the errors expected to go away at this point. This is not right. It's not set. That's why we're having the. The error. We chose the wrong thing.
This is mutable state. Off. And then we have. Okay. I'm going to. Import that. Then check it again. And import that. And all the errors expected to go away at this point. Okay. I'm going to add that. So. Without wasting time. We're going to look at. What we have. Achieved at this point. Now. I can add some modifiers here to help this up.
Oh, yeah. Okay. I'm going to add that. So. Without wasting time. We're going to look at. What we have. Achieved at this point. Now. I can add some modifiers here to help this up. Our UI. Look. More awesome. Like. If you can see from my Android studio. From the preview update. You see. Let's check. Update that. And see.
What we've done. At this point. Okay. Let's. Run it on a physical device. So you see. What we have already. Achieved here. Just give me some minutes. We run that on this physical device. And then. See what we've been able to achieve. So. But without wasting time. Without. Without wasting time. I can add an alignment for. The rules.
So that. Everything can stay. The way they're supposed to be. And even also. Add modifiers. To the column. So that. The size. Will cover the screen. Of the. That. We. Fixed very well. On the screen. I can add. Modifiers. To. I told you. What modifiers. Are useful. So I. Can. Over here. Add. I can use. Modifier. Right.
To add. The padding. Okay, I can use modifier to, I wonder why that's giving me that error, but that's the right thing to do. Okay, so I do this, modifier, I have modifier, I want it to, I want the screen to fill the size that what I have on this to fill the size of the screen. Modifier dots, I will use fill max size, then I'll use one, okay, I can also add more, I can also add padding, padding, then padding, let me say padding start, padding start, which is, let's say, sys.dp, and I can add the end, okay, which is, let me also use sys.dp, okay, so we'll move ahead to work on this, so we want to go ahead, the next thing is to add the button, at this point, I can use a row, because you see,
you see, they were arranged horizontally, I told you that, rows are also used, yeah. So I arrange things, I can pull this, I prefer doing it that way, there are many other ways you can do that. I want to add row, okay, so in this row, I will add the board, this, then I will add the button, I like outline, so I'm going to use outline.
line button. Okay, here in that line button, I'm going to use a composable element test, so I will be able to add, add. Oh no, there'll be an error there. So I say add. Awesome. So let's check what we have worked on on a physical device to see. exactly what we are building, how it will look like on a physical device.
But over here on the preview section, you can see exactly what we are trying to achieve. but I'm going to connect a physical device at once, looking at our time, and we can go ahead, connect a physical device also, so we can see. So if I put down the Android Studio on my slide, I can pull that up again, you will see that the next thing we need to achieve, what we've achieved is this.
So we want to achieve adding how do we add list. Okay, list on, on, on, on using compose. Now at this point, I would like us to compare adding list using Kotlin and then using compose. And compose is just very, very simple to do that. If you look at, in Kotlin, you have to use RecyclerView. Okay. RecyclerView and Jetpack Compose are both tools that can be used to display list in Android application.
But they use different approaches to achieve that. That's one thing about, they use different approaches to achieve that. Now let's look at, to achieve this. Okay. Let's look at RecyclerView. For RecyclerView is a UI component in Android Studio that is used to display a large dataset in a list or a grid format. It is a part of what's called Android framework.
And it's implemented in Java and also in Kotlin. Okay. It's used, it's used with what? An adapter. This adapter provides data to be displayed and handled. And handles the, handles the creation and then data view. Okay. Now to use RecyclerView in Kotlin, you would create a RecyclerView in your layout XML file. And then create another class. There's adapter class now that extends the RecyclerView adapter.
The adapter class would define the data that's to be displayed and handle the creation and binding of the views. Okay. You would then set the adapter on the RecyclerView in your Kotlin code. But in Compose, it's just very simple and straight to the point. In Compose, you just have, Compose provides more concise and intuitive way to build UI compared to the traditional Android layout.
In Compose, in Jetpack Compose, you know, you would create a composable function that defines the UI for your list and use the lazy column or lazy row layout to display the list. Everything, you don't need to create another class or anything. Everything can be done in that just one class we created. One file we created. We created, okay.
The lazy column or lazy row layout creates and binds the view as needed. Okay. Which makes it more efficient than the traditional way of using adapter and RecyclerView and the rest of them. If you have time, I'm going to show you exactly how this can be done. But now let's look at using RecyclerView and then using Compose.
In terms of ease of use and then the development speed, Jetpack Compose is easier and faster to develop compared to RecyclerView. This is because, you know, Jetpack Compose, what it does is that it uses a more concise and intuitive syntax. I've mentioned this before. And it has the creation and the binding of views automatically, unlike using, what's it called, using RecyclerView.
Okay. Now, when you talk about in terms of performance also, Jetpack Compose is also considered to be more efficient compared to RecyclerView, okay. Jetpack Compose uses more efficient layout system. That's one thing about it. that creates and bind or confuse needed, which reduce memory or usage and then improve performance. However, the performance of Jetpack Compose may depend on the complexity of the UI and the number of the item you want to list on that UI.
So that you're also going to take note into consideration why when you're working on your app. So I'm going to, having said that, I'm going to pull out Android Studio again so we can continue if we still have the time. And don't forget, if you have any questions for this session, feel free to drop it on the chat section.
I will attend to that if you have the time. Okay, now what I'm going to do at this point is to do what is to create a list, show you how to create a list using Jetpack Compose. So what we need to do, we want to display the list of courses, okay. So I'm going to start by doing this.
I like using a card, okay, so to not have it model up to look nice. So what I just need to do is to create a composable function. I will say Compose, this make it faster for me. And I'll call it a course, what card. I prefer using cards so that I have things neat and more organized on my, on my, on my UI.
So I'm going to use. I said earlier, trust me. I like using outline. I'm going to use an outline card also here. Okay. All I need on this card is just, just the card. That's all I need. It's just the card. I don't need a lot of things on the card. So I just create the card and then make use of it.
But then I'm going to have add a parameter to this card. And the parameter, what is going to take, will be course title. In a small letter, course title. Let's also course title, look at the name conversion when we are building an Android application. And this course title is going to be a string. Okay. So over here, I'm going to create, use a test.
I mean, a composable element. Oh no. I typed the wrong thing. I'm going to use a composable elements. Which text the string I have over there. That string is what course title. This is what this is going to take. So the next for me to go about. I'm going to, I'm just looking at my time. Is what the next thing to do is to use, create another composable function.
And this composable function would be for a lazy column. Because we want to display a list. Okay. Let me send this to a physical device. So we can see how it look. I can pull out a physical device. This physical device. We can see it here. Put that out again. Until I can see it here. Okay. The next thing is to use.
Create another composable function. For a list. I'll call this lazy. Let's say learn. Okay. Learn. Cause. Lazy. Let's say learn. Okay. Learn. Cause. Lazy. Let's say learn. Okay. Learn. Cause. Lazy list. That will help us to. Okay. So in here. Now I'm going to use. Call. My lazy list. Lazy. I mean, not lazy list. Lazy column. To display.
The list. The list of the items. I don't need this. I'll go for here. Awesome. So. The next is. I'm going to use. My lazy list. Lazy list. I mean, not lazy list. Lazy column. To display. The list of the items. I don't need this. I'll go for here. Awesome. So. The next is. Okay. To create the list.
The list of the item that I need. At this point. So how do I do that? I'm going to create what? A variable. And this variable should be able to accept list. And I'm going to call that variable. I like rating it up here. Where did I create the first one here? I have. A variable. That's not right.
Variable. Wow. Let's call it cost. List. Plus list. And this. Remember. Using this state. Remember what I said about state. I will also remember. At this point. Because this is a list. Okay. I'm not just going to use. I'm not. I'm not going to use. Mutable state list. At this point. Okay. Because this is a list. I'm not just going to use.
I'm not going to use. Mutable state list. At this point. Okay. I'm not just going to use. I'm not going to use. Mutable state list. At this point. Okay. Now. What I'm going to use. To get. Exactly. What I'm looking for. Which is. Is. Mutable state list. Of. Which would take a parameter. String. So. Let's add that.
Over here. We'll have. Mutable. Mutable states. List. Of. Type again. So. And make sure I got the right one. Because there's. There are a lot of them. Mutable states. List. Of. Make sure it. Accept. The. Over here. A string. Parameter. Okay. Yeah. That is. Awesome. So. We'll come back to the lazy column. And we're going to add this up.
For us to see what I'm working on over here. I'm going to call this lazy list over here. okay yeah that is awesome so we come back to the lazy column and we're going to add this up so for us to see what i'm working on over here i'm going to call this lazy list over here in my in the column so that everything is going to be arranged the way we want it okay here inside here i add it remember i can use a spacer so everything is not modeled up together use a spacer and then add some modifier to give it a padding and then i can say just sysdp sys.dp okay then i'm going to call the list the uh learn course
right learn course list that's what i need over here okay so we complete this now we already have a card we have a course title and then we have uh we have created a variable for our lazy column so the last thing we need to do just to add up everything in the lazy column and we'll be ready to to go so the lazy column will take item that different items we want to see in the list okay so i can use items items with s this that can take list okay then i will pass the cost list variable over here i will pass okay i will pass okay i'm using capital letter and that would take um um okay for one list you know why that didn't work because this cost uh learn only a cost list
doesn't have the parameter i'm trying to pass that's why when i try to pass the variable that i've already created over here it didn't work so i'm going to add that parameter over here and then this what i have yes it's a string so in this item i can then pass this over here then we're going to get it one after the other okay each list so i can say cost that's for one cost um add user lender and then i'm now going to call this course card this course card will help me to get all the courses when it's been added in the test uh field so i'm going to call course card here and this is going to take just one course oh no no i gotta spend okay
okay it needs int i'll create this okay let's fix that now what it depends on what i use over here the course i use i'm going to comment that the item the type of the item i i used if i select the wrong items because that many of them what you understand about uh compose is that compose has a lot of inbuilt functions when you select the wrong inbuilt function to use you are automatically going to get error an error okay so if you use the like the right one you get it right but another thing again is here over here there's something going on we are trying to get list of items but if i should use this just string for the parameter that will cost a lot of errors so what i supposed to use i supposed to use list to tell
this that what i'm trying to get is a list and then i'm trying to get list of string at this point so with that if i recommend this everything will just be normal awesome i'm having an error somewhere so let's find okay this error is saying that you must pass something over there and what do you need to what do we need to pass over there is the list is the cost list that we are trying to get so we have course course that's not course list so at this point you will see that everything is great now we're going to show exactly what we have worked on to see if actually this we are done but trust me we're not done we're not done because we've not added anything for the on click for the button so that thing it will be
easier for us when you add a value and it will be uh you will immediately see but with that coming on let's just have a look at where we've gotten to at this point and going to run this i'll close this so we have it in the real life uh device okay so what are we going to do at this point so we can be able to manage the click action at this place okay so we're going to tell the test the test field what we want what we want and i'm going to use an if statement to tell the test field that if a test value is not does that on the test field if a test uh value is not uh empty that you called what
costless and then add a test value i can also yes that's exactly what we want to tell the uh the click button to do for us the button to do for us okay so at this point we're going to see if um okay give me some minutes like uh the android studio is running on the fish trying to build on a physical device but we can't wait for that to finish so we move ahead if okay that's like interrupting what's going on in the android studio don't worry that will be that will come up soon okay is building the android studio takes some time uh to build and to complete its build that's what is going on here so it's building with griddle uh but i wonder why it's taking this time it's actually faster
okay okay let's continue and accept this to happen okay so we're going to say the test value dot is not is not empty if it's not empty we're trying to tell uh we're trying to tell the but try to say that the okay we say we use cost list we call cost list dots add what is it going to add is going to add cost so that's exactly what is going to add here we're still holding on on the android studio complete to complete its uh build it will add the new a new what sext value that's exactly what we wanted to do okay that has been done and yes we accept the store so install i'll make this big and we'll wait for the app to be installed
okay now it is blank you know why because and the main activity will never call this function over there in the main activity see is is open it's left open in the main activity so all we need to do is to come back here and call the function learn of course here and we will run this app so at this point we're going to see what we've been able to achieve in this session so if you have a question feel free to okay to ask and as soon as this is done we've achieved our aim it has the will be the in the wrap up for this post just a few minutes the android studio sometimes take time i don't know why taking time today normally it comes up uh very fast
okay but bear with it awesome this is what we've been able to achieve so let's see if we can be able to add anything on the test field let's add uh android android course let's add and see what happens awesome we have the list if we have android okay i'm going to handle that you notice that when i typed and i was trying to add another one what happened every the first test i typed was there still there so for me to handle that i'm just going to add i'm just going to add just a simple just a simple uh thing to add at this point here okay i just say that this test value should be empty after adding the first typing in in to give me an empty string after typing in
the first one if you rerun this again i'm going to get what i'm looking for here now there's a lot of things we still need to do to modify to make our app everything look better we can modify the text where this text is coming from this test on over here you can modify this text here and add a lot of add a lot of things i can add padding around it let's say modifier can use modifier to add to beautify it make it fine um say um modifier say modifier modifier but pardon oh that's padding i can't see it this is it um it's uh dp okay so if we run this app we'll get exactly what we are looking for so thank you so much everyone for joining my session i'm so glad to have you and your time thank
thank you for your patience also i'm going to say thank you again for women take make us for uh moving to take global conference this 2023 for giving me the opportunity so to be able to take this session um let's uh add courses now see how our final results what we have as final android course list one um i want to get more of that so you can observe awesome or you i post list again let me say cost list two exactly another one you see that go join together because i didn't add padding on the card to separate them i can also add padding to separate them so that they are just on their own so thank you so much for joining my session and my name remains a peculiar to me if you have any other question after this
session or you won't have want us to connect after this um session you can reach out to me on um social media connect with me on linkedin or on twitter or instagram with this name thanks so much for joining my session bye and see you next time you can also want us to build a project together um available at all time to contribute to any new projects and walk you through thank you so much for joining this session bye um you
Transcribed automatically; names checked against the speaker record. Select a time to jump there.
Article based on this talk
Wie Jetpack Compose XML-Layouts und RecyclerView-Adapter durch reines Kotlin ersetzt
Peculiar C. Umeh baut live eine Kurslisten-App und zeigt die zentralen Compose-Konzepte, die neue Android-Entwickler brauchen.
Kurz gesagt
Mit Jetpack Compose beschreiben Android-Entwickler ihre UI in Kotlin-Funktionen statt in XML-Layouts, und Listen funktionieren ohne separate Adapter-Klasse. In einer praxisnahen Session baut Android-Entwicklerin Peculiar C. Umeh eine kleine App, in der Nutzer Kursnamen eingeben und diese in einer Liste erscheinen sehen. Dabei führt sie Composable-Funktionen, Previews, Modifier, Layouts, State und LazyColumn ein. Ihr Fazit: Compose ist einfacher, schneller in der Entwicklung und in den meisten Fällen effizienter als der RecyclerView-Ansatz.
Die wichtigsten Erkenntnisse
Ein Android Studio-Projekt aus der Vorlage „Empty Compose Activity“ mit Kotlin 1.5 oder neuer aufsetzen.
Composable-Funktionen schreiben und sie mit der Preview-Annotation sofort prüfen.
Modifier sowie die Layouts Column und Row nutzen, um UI-Elemente zu gestalten und anzuordnen.
TextField-Eingaben mit remember und Mutable State verwalten, Listen mit einer Mutable State List.
Eine RecyclerView samt Adapter durch eine LazyColumn aus Card-Composables ersetzen.
Ein deklaratives Toolkit und der Einstieg
Umeh stellt Compose zu Beginn als modernen Weg vor, native Android-Oberflächen zu bauen. „Mit Jetpack Compose können Sie mithilfe eines deklarativen Programmiermodells schöne, responsive, flexible Benutzeroberflächen erstellen.“ (0:30)
Die Session bleibt praxisorientiert. Voraussetzungen sind ein aktuelles Android Studio (sie arbeitet mit Flamingo) und Kotlin 1.5 oder neuer. Ein neues Projekt aus der Vorlage „Empty Compose Activity“ liefert die nötigen Abhängigkeiten und ein Start-Composable, das „Hello Android“ anzeigt. Als der erste Build fehlschlägt, zeigt sie eine gängige Lösung: die Gradle-Einstellungen auf Java 17 umstellen und neu bauen. Eine realistische Erinnerung daran, dass die Einrichtung der Tools Teil der Lernkurve ist.
Composable-Funktionen und Live-Vorschau
Die Kernidee von Compose ist die Composable-Funktion. „Composable-Funktionen sind die Bausteine der Jetpack Compose-UI.“ (6:11) Jede ist eine mit einer Annotation markierte Kotlin-Funktion, die ein Stück UI erzeugt. Umeh stellt dem die XML-Entwicklung gegenüber, bei der ein einfacher Bildschirm gar keine Annotationen benötigt.
Anschließend erstellt sie ihr eigenes Composable für die App, legt es nach guter Praxis in ein Presentation-Package und fügt eine Preview-Annotation hinzu, damit Android Studio die UI schon beim Tippen rendert. Ohne diese Annotation, betont sie, zeigt der Vorschaubereich nichts an. Außerdem führt sie die App auf einem physischen Gerät aus, um zu zeigen, dass die Vorschau dem entspricht, was Nutzer sehen werden.
Modifier und Layouts
Um Aussehen oder Verhalten eines Elements zu ändern, nutzt Compose Modifier. „Modifier sind Funktionen, die mit dem Punktoperator auf einem Composable-Element aufgerufen werden.“ (11:31) Umeh setzt damit Schriftgröße und Padding und lässt später eine Column den gesamten Bildschirm mit Padding an jeder Seite ausfüllen. Zudem nennt sie eine praktische Gewohnheit zum Entdecken: Wer in Android Studio mit der Maus über ein Composable wie ein Text-Element fährt, sieht alle Parameter, die es akzeptiert.
Für die Anordnung mehrerer Elemente sind Layouts zuständig. „Layouts werden verwendet, um Composable-Elemente auf dem Bildschirm anzuordnen. Es gibt mehrere integrierte Layouts in Jetpack Compose.“ (22:27) Mit einer Column stapelt sie Überschrift, Spacer und Eingabebereich vertikal, mit einer Row platziert sie ein Outlined Text Field und einen Outlined Button nebeneinander, passend zum Mock-up, das sie zu Beginn gezeigt hat.
State: das Textfeld zum Laufen bringen
Das Outlined Text Field zeigt sofort Fehler, die Umeh als Lerngelegenheit nutzt. Ein TextField benötigt einen value (die aktuelle Eingabe) und ein onValueChange-Lambda, das bei jeder neuen Eingabe aufgerufen wird. Das führt zu State, den sie weit definiert: „Es ist jeder beliebige Wert, der sich in Ihrer Anwendung im Laufe der Zeit ändern kann.“ (26:35)
Sie speichert die Eingabe mit remember und Mutable State, verbindet das Lambda zur Aktualisierung und arbeitet sich live durch Import- und Typfehler, wobei sie aus den vielen Vorschlägen von Android Studio die passende integrierte Funktion wählt. Ihr Rat: keine Angst vor den roten Linien haben, sondern lesen, was sie sagen.
LazyColumn versus RecyclerView
Bevor sie die Liste baut, vergleicht Umeh beide Ansätze. Bei RecyclerView deklariert man sie in einem XML-Layout, schreibt eine Adapter-Klasse, die Views erstellt und bindet, und hängt den Adapter im Kotlin-Code an. In Compose kann ein einziges Composable die Liste definieren. „Das LazyColumn- oder LazyRow-Layout erstellt und bindet die View nach Bedarf.“ (38:50)
Sie argumentiert, dass Compose bei Benutzerfreundlichkeit und Entwicklungsgeschwindigkeit vorne liegt und allgemein als effizienter gilt, weil es den Speicherverbrauch reduziert. Dazu ergänzt sie eine ehrliche Einschränkung: „Allerdings kann die Performance von Jetpack Compose von der Komplexität der UI und der Anzahl der Elemente abhängen, die Sie in dieser UI auflisten möchten.“ (40:17)
Um die App fertigzustellen, erstellt sie ein Kurs-Card-Composable mit einer Outlined Card, hält die Kurse in einer Mutable State List aus Strings und rendert sie mit LazyColumn-Items. Der Klick-Handler des Buttons fügt den eingegebenen Kurs hinzu, wenn das Feld nicht leer ist, und leert anschließend das Feld. Nachdem sie das Composable aus der Main Activity aufgerufen hat, läuft die App auf ihrem Gerät, und jeder neue Kurs erscheint als Card in der Liste.
Den Talk ansehen
Building Android Applications with Jetpack Compose, von Peculiar C. Umeh, 64 Minuten: https://www.womentech.net/video/building-android-applications-jetpack-compose
Über die Referentin
Peculiar C. Umeh ist Android-Entwicklerin in Port Harcourt, Nigeria. Ihre Session konzentriert sich auf Android, Kotlin und Jetpack Compose.
Fragen, die dieser Talk beantwortet
Was brauchen Sie, um eine App mit Jetpack Compose zu entwickeln?
Was ist eine Composable-Funktion?
Wie funktionieren Modifier in Jetpack Compose?
Wie verfolgt ein TextField, was der Nutzer eingibt?
Wie schneidet LazyColumn im Vergleich zu RecyclerView bei Listen ab?
Mit KI aus dem Transkript des Talks erstellt. Redaktionelle Prüfung ausstehend.
Frameworks and ideas mentioned
Declarative UI programming model
Resources mentioned
Android Studio (Flamingo)
Empty Compose activity template in Android Studio
Cite this talk
Peculiar C. Umeh. "Building Android Applications with Jetpack Compose." WomenTech Network, 2023. https://www.womentech.net/video/building-android-applications-jetpack-compose