Material Buttons in Kotlin KMP Compose Multiplatform

In today’s tutorial, we will discover the complete guide to Material buttons in Kotlin KMP Compose Multiplatform. We will learn about the Simple, Outlined, Text, and Elevated buttons, their properties, and onClick listeners for Android and iOS applications.

Material Buttons in Kotlin KMP Compose Multiplatform

Material Buttons in Kotlin KMP Compose Multiplatform

Buttons are essential UI components in every mobile, web, and desktop application. They allow users to perform certain actions by tapping on a fixed, predefined area on the UI screen. For example, users can tap a button to call a function, call an API, store data in a database, Navigate to the next screen, or perform any other required task.

There are the following types of Button widgets available in KMP ComposeMultiplatform:

  1. Button
  2. OutlinedButton
  3. TextButton
  4. ElevatedButton using Simple Button properties

Common properties in all button widgets:

  1. onClick: The onClick method calls a function when the user taps the button. The button click body is defined using { } curly brackets, so you can call multiple line codes directly into this block.
  2. modifier: The modifier sets button width, height, and padding and applies different UI properties. I have already made a tutorial explaining all modifier properties. You can read the tutorial from HERE.
  3. enabled: The enabled property enabled and disabled the button. To enable, you have to pass true, and to disable the button, pass false. It can also be controlled by State management. So, for certain events, the button will be enabled.
  4. shape: Define a button shape like a simple rectangle or rounded corner-shaped button.
  5. colors: These are used to define the button background colour and the button inside text content colour.

1. Material Button:

The Button widget in KMP is used to create a Material style button.

Output:

Simple Button in KMP

2. OutlinedButton:

The OutlinedButton automatically creates a button with a border around it. We will use the BorderStroke(2.dp, Color.Red) property to create the border. In place of colour constants, you can also use HEX and ARGB colour codes.

Output:

Outlined button in KMP

3. TextButton:

Text buttons are simple String text buttons. When a click listener is required to apply on Text, it is used.

Output:

Text Button in KMP

4. Elevated Button:

The elevated button can be created easily using a simple button widget by applying the elevation property. It makes a drop shadow effect beneath the button. Applying the elevation property creates an elevated button within.

Output:

Elevated Button in KMP

Complete source code for App.kt file:

Screenshot:

Material Buttons in Kotlin KMP Compose Multiplatform

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *