Jetpack Compose: Button, Outlined Button, and Text Button in Android
In this article, you will learn to create Button(), OutlinedButton() and TextButton() composables and how to customise their appearance such as style, size, elevation, border stroke color, background color, content colour, and apply onClick listener to it.
Source code link at the end of this article.
Don’t like reading? Watch the video instead 👇 OR click here to watch
Ok, let’s start the tutorial.
In general, there are three types of Buttons provided by Material Design Component library for Jetpack Compose.
1. Button(): A normal and regular button
2. OutlinedButton(): Transparent background but with an outline
3. TextButton(): A normal button that appears like a text
These three buttons can optionally contain an Icon as shown in the pic below.
How to create a Normal Button?
We can use Button() composable for that.
The above code 👆 will render a simple button. To add text we are using the Text() composable widget as the content of the Button.
- The onClick parameter is mandatory. It expects us to pass a lambda function.
How to add an icon to the Button?
We can use Icon() composable for that.
The Icon() composable expects us to define at least two parameters: imageVector and contentDescription.
- imageVector: Mandatory. Provide a reference to the image. I am using a vector image from Android SDK.
- contentDescription: Mandatory. Provide what type of image you are using. This is important to make your app accessible (accessibility feature). You may use null as its value if you don’t want it.
- modifier: Optional but important. I am using it to resize the image as per the standard size of the Text being used in the Button. If you don’t use it, your icon will appear a little bigger than the standard text size.
Additionally, I have removed the comment from onClick and replaced it to display a Toast message. You can get the source code in the end.
- ButtonDefaults is an object declaration that contains default values which can be used by developers without thinking too much about what values to use. Such as ButtonDefault.IconSize and ButtonDefaults.IconSpacing are pre-defined constants — with dp values of 18 dp and 8 dp respectively.
What is the purpose of Spacer() composable?
The Spacer composable represents empty space. In our code, we have used it to add some space between the Icon and the Text.
How to create OutlinedButton?
It’s really simple. Just replace Button with OutlinedButton composable.
How to create TextButton?
Again very simple. Just use TextButton composable.
How to customise a Button, OutlinedButton, and TextButton?
These three buttons can be customised using the same parameters. i.e. same parameters can be used with all the three types of buttons. Let’s see examples.
The following is an example of a simple Button with a width of 200dp, elevation of 10dp, background color of Black, and content color of White.
The above parameters can be used with OutlinedButton and TextButton. Test yourself, how they look with exactly the same values of parameters.
Similarly, if you want an OutlinedButton with a border stroke, then you can make use of the border parameter:
In this way, you can use any type of parameter with any type of button. Just change the values and get the result.
Get the source code from Github: Click Here . Give it a STAR ⭐
One clap, two claps, three claps…. Forty! 👏👏👏👏 💕
If you want video tutorials, subscribe to my youtube channel Smartherd.
100 thousand already did. Don’t wait!