Coding Bihar

Android App Development

Coding Bihar is dedicated to providing you with the knowledge and tools you need to excel in Android development. Start your journey with us and transform your app ideas into reality with Jetpack Compose. Connect, learn, and grow with Coding Bihar today!

Buttons in Jetpack Compose

Buttons in Jetpack Compose - Coding Bihar
Buttons in Jetpack Compose

Different types of buttons in Jetpack Compose

How to create buttons using jetpack compose?
Buttons are UI Components used to apply on Click action. The different types of buttons used in app's UI are: Icon Button, Text Button, Outlined Button, Elevated Button, Filled Button, Tonal Button, Toggle Button, Segmented Button, FAB, Extended FAB(Floating Action Button).

Icon Button - Icon Button is a simple button containing an icon which is use to explain the type of action that button respond on clicking.
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
 IconButton(onClick = {Toast.makeText(context,"",Toast.LENGTH_SHORT).show() },Modifier.width(180.dp)) {
            Row {
                Icon(imageVector = Icons.Default.Place, contentDescription = "IconButton")
                Text(text = "IconButton")
            }

        }
   }
Text Button - This type of  button displays text without any background or border. 
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
TextButton(onClick = { /*TODO*/ }) {
            Text(text = "TextButton")
        }
   }
Outlined Button - This is type of button with a border around
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
  OutlinedButton(onClick = { /*TODO*/ }) {
            Text(text = "OutlinedButton")
      }
   }
Elevated Button - An elevated button has a shadow showing 3D effect that appear even more prominently.
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
ElevatedButton(onClick = { /*TODO*/ }) {
            Text(text = "Elevated")
        }
  }
Filled Button - Filled button contains Solid background with contrasting text.
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
FilledIconButton(onClick = { /*TODO*/ }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place")

        }
   }
Tonal Button - Tonal button contain a lighter background color and darker label color, making them less visually prominent than a regular, filled button.
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
   FilledTonalButton(onClick = { /*TODO*/ }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place")

        }
   }
Toggle Button - Android Toggle Button is used to display on and off state on a button as a switch.
Copy this code →

@Composable
fun TypesOfButton() {
var checked by remember {
      mutableStateOf(true) // initially checked
        }
 IconToggleButton(checked = checked, onCheckedChange = {
            checked = it
        }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place",
                tint = if (checked) Color.Red else Color.Black
            )
        }
  }
Segmented Button - Segmented buttons is a group of buttons  help people select options, switch views, or series of elements.
Copy this code →

@Composable
fun TypesOfButton() {
val context= LocalContext.current
 } 
      
      
Now here is code for all types of button which we can create using jetpack compose al together is
Copy this code →

package com.example.jetpackcomposeskcoding

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.Button
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.FilledIconButton
import androidx.compose.material3.FilledIconToggleButton
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.FilledTonalIconButton
import androidx.compose.material3.FilledTonalIconToggleButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedIconButton
import androidx.compose.material3.OutlinedIconToggleButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

@Composable
fun TypesOfButton() {
    val context= LocalContext.current
    Column (Modifier.padding(20.dp)){
        var checked by remember {
            mutableStateOf(true) // initially checked
        }
        Text(text = "Button", color = Color.LightGray)
        Button(onClick = { Toast.makeText(context,"Button",
            Toast.LENGTH_SHORT).show()},
            Modifier.width(180.dp)) {
            Text(text = "Button")

    }

        Text(text = "Icon Button", color = Color.LightGray)
        IconButton(onClick = { Toast.makeText(context,"Icon Button",
            Toast.LENGTH_SHORT).show()},
            Modifier.width(180.dp)) {
            Row {
                Icon(imageVector = Icons.Default.Place, contentDescription = "IconButton")
                Text(text = "IconButton")
            }

        }
        Text(text = "Icon Toggle Button", color = Color.LightGray)
        IconToggleButton(checked = checked, onCheckedChange = {
            checked = it
        }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place",
                tint = if (checked) Color.Red else Color.Black
            )
        }
        Text(text = "FilledIconButton", color = Color.LightGray)
        FilledIconButton(onClick = { Toast.makeText(context,"Filled Icon Button",
            Toast.LENGTH_SHORT).show()}) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place")

        }
        Text(text = "FilledTonalButton", color = Color.LightGray)
        FilledTonalButton(onClick = { Toast.makeText(context,"",
            Toast.LENGTH_SHORT).show() }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place")

        }

        Text(text = "FilledTonalIconButton", color = Color.LightGray)
        FilledTonalIconButton(onClick = { Toast.makeText(context,"Filled Tonal Icon Button",
            Toast.LENGTH_SHORT).show() }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place")
        }

        Text(text = "FilledTonalIconToggleButton", color = Color.LightGray)
        FilledTonalIconToggleButton(checked = checked, onCheckedChange =  {
            checked = it
        }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place",
                tint = if (checked) Color.Red else Color.Black
            )

        }

        Text(text = "FilledIconToggleButton", color = Color.LightGray)
        FilledIconToggleButton(checked = checked, onCheckedChange = {
            checked = it
        }) {
            Icon(imageVector = Icons.Default.Place, contentDescription = "Place",
                tint = if (checked) Color.Red else Color.Black
            )

        }

        Text(text = "OutlinedButton", color = Color.LightGray)
        OutlinedButton(onClick = { Toast.makeText(context,"Outlined Button",
            Toast.LENGTH_SHORT).show() }) {
            Text(text = "OutlinedButton")
        }

        Text(text = "OutlinedIconButton", color = Color.LightGray)
        OutlinedIconButton(onClick = { Toast.makeText(context,"Outlined Icon Button ",
            Toast.LENGTH_SHORT).show()},
            Modifier.width(200.dp)) {
            Row{Icon(imageVector = Icons.Default.Place, contentDescription = "Place")
            Text(text = "OutlinedIconButton")}
        }

        Text(text = "OutlinedIconToggleButton",
            color = Color.LightGray)
        OutlinedIconToggleButton(checked = checked,
            onCheckedChange ={
        checked = it} ){
        Icon(
            imageVector = Icons.Default.Place,
            contentDescription = "Favorite Item",
            tint = if (checked) Color.Magenta else Color.LightGray // icon color
           )
        }

        Text(text = "TextButton", color = Color.LightGray)
        TextButton(onClick = { Toast.makeText(context,"Text Button",
            Toast.LENGTH_SHORT).show() }) {
            Text(text = "TextButton")
        }

        Text(text = "ElevatedButton", color = Color.Gray)
        ElevatedButton(onClick = { Toast.makeText(context,"Elevated Button",Toast.LENGTH_SHORT).show() }) {
            Text(text = "Elevated")
        }
    }
}

Output:
Different types of buttons using Jetpack Compose

div>If you want to customized a button with background color, elevation, content color and shape that is default then here is a code
Copy this code →

package com.example.jetpackcomposeskcoding

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Face
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.FilledIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedIconButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.jetpackcomposeskcoding.ui.theme.JetpackComposeSkCodingTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeSkCodingTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Buttons()
                }
            }
        }
    }
}

@Composable
fun Buttons() {
    val context= LocalContext.current
    Box(modifier = Modifier.size(310.dp).padding(30.dp)) {
        Button(onClick = { Toast.makeText(
            context, "I am customized button build by jetpack compose",
            Toast.LENGTH_SHORT).show()}, shape = RectangleShape,
            elevation = ButtonDefaults.buttonElevation(6.dp),
            colors = ButtonDefaults.buttonColors(containerColor = Color.Green,
                contentColor = Color.Red)) {
            Row {
                Icon(imageVector = Icons.Default.Face, contentDescription = "Icon Button")
                Text(text = "Simple Customized Button", color = Color.Black)
            }
        }
    }
}
Output:
Customized Button using jetpack Compose


Do you want to know how to create FAB using jetpack compose click below
 Sandeep Gupta

Posted by Sandeep Gupta

Please share your feedback us at:sandeep@codingbihar.com. Thank you for being a part of our community!

Special Message

Welcome to coding bihar!