Creating a Custom Microsoft Icon with Jetpack Compose

Esther Carrelle Rangira
3 min readJul 5, 2023

--

Jetpack Compose is a modern toolkit for building native Android UI. It provides a declarative API that makes it easy to create complex user interfaces. In this article, we will create a custom Microsoft icon using Jetpack Compose.

Prerequisites

To follow along with this tutorial, make sure you have a basic understanding of Jetpack Compose

The first step is to create a new project in Android Studio.

Create new project
Project description

Once the project is created, we need to add the following dependency to the build.gradle file:


implementation 'androidx.compose.material3:material3'

Code Breakdown

The MicrosoftIcon component is a composable function annotated with @Composable, indicating that it can be used within Compose UI hierarchy. It sets up a canvas to draw the icon using the Canvas composable.

@Composable
fun MicrosoftIcon(modifier: Modifier = Modifier.size(100.dp)) {
Canvas(modifier = modifier) {
// Icon drawing code goes here
}
}

Drawing the Icon: The icon is composed of multiple colored rectangles. Each rectangle is drawn using the drawRect function.

@Composable
fun MicrosoftIcon() {
Canvas(modifier = Modifier.size(100.dp)) {
val width = this.size.width
val height = this.size.height

drawRect(
color = Color(0xFFF25022),
size = Size(width * .50f, height * .45f),
topLeft = Offset(width * .0f, height * 0f)
)
drawRect(
color = Color(0xFF7FBA00),
size = Size(width * .50f, height * .45f),
topLeft = Offset(width * .55f, height * 0f)
)

drawRect(
color = Color(0xFFFFB900),
size = Size(width * .50f, height * .45f),
topLeft = Offset(width * .55f, height * .50f)
)

drawRect(
color = Color(0xFF00A4EF),
size = Size(width * .50f, height * .45f),
topLeft = Offset(width * 0f, height * .50f)
)
}
}

In this code snippet, four rectangles are drawn with different colors. Each rectangle is defined by specifying the color, size, and position. The Color class is used to set the desired color, and the Size class defines the dimensions of the rectangle. The Offset class determines the top-left position of each rectangle within the canvas. By varying these parameters, you can customize the appearance of the icon.

Conclusion

In this article, we explored the code snippet for creating a vibrant icon component called MicrosoftIcon using Jetpack Compose. By leveraging the Canvas composable and the drawRect function, we constructed a visually appealing icon consisting of multiple colored rectangles. With Jetpack Compose's declarative and flexible approach, it becomes effortless to create custom UI components that enhance the visual appeal of your Android applications.

By understanding the concepts demonstrated in this article, you can adapt and extend the provided code to create your own unique UI components. Let your creativity flourish and create stunning visuals with Jetpack Compose. Happy composing!

Feel free to connect with me and follow my github account.

https://www.linkedin.com/in/esther-carrelle-rangira/

--

--