The Programmers Guide To Kotlin - Compose Layout
Written by Mike James   
Tuesday, 02 July 2024
Article Index
The Programmers Guide To Kotlin - Compose Layout
Getting Started
Content and Layout
A Simple Layout Example

Compose Multiplatform is a great way to create a UI for a range of platforms. In this extract from the book on Kotlin by Mike James we look at how to understand how its declarative approach is different.

{loadposition kotlinContents}

Although you can use Java libraries to create a user interface, UI, Kotlin has its own approach to the problem in the form of Compose Multiplatform. This is a declarative system that allows you to create a UI that works on Android, iOS, Desktop (Windows, Linux and MacOS JVM) and web (JS/WASM). At the time of writing, the iOS version is in beta and Web is in alpha. Under Android, Compose Multiplatform is called Jetpack Compose.

There is also a version of Multiplatform which doesn’t use Compose to create a UI but produces individual native implementations. This approach allows you to share non-UI code between platforms and craft custom UI code for each platform you want to support. Multiplatform is simpler, but Compose Multiplatform is a more efficient approach.

Compose is also being advocated by the Android team as the best way to create Android apps – it is part of the move to the so-called “Modern Android Development”, or MAD. This makes it even more attractive as a way of developing a cross-platform app.

However, all being said, Compose can be difficult to understand at first. Especially so if you have been deep into traditional procedural UI implementations, such as Swing or the Android or iOS UI. Compose is declarative and makes use of observable objects and coroutines to allow updates to the UI and doesn’t give you direct access to UI attributes.

This is a different way of thinking and it takes time to get used to it. In this chapter you will meet the things that make Compose different. To fully understand it you need to make sure that you are up to speed with delegation using the by keyword, covered in Chapter 6, compact lambdas, Chapter 11, and that you understand basic coroutines, Chapter 14.

Last Updated ( Tuesday, 02 July 2024 )