Skip to content

Instantly share code, notes, and snippets.

@rubywai
Created December 14, 2025 12:28
Show Gist options
  • Select an option

  • Save rubywai/5bdc50ab8793bb6ee849d3537a2f9f9f to your computer and use it in GitHub Desktop.

Select an option

Save rubywai/5bdc50ab8793bb6ee849d3537a2f9f9f to your computer and use it in GitHub Desktop.
Understanding Material Buttons in compose
package com.rubylearner.mycomposeapp
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Badge
import androidx.compose.material3.BadgedBox
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.rubylearner.mycomposeapp.ui.theme.MyComposeAppTheme
//Scaffold related compose
//Row Column
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MyComposeAppTheme {
Scaffold(
topBar = {
CenterAlignedTopAppBar(
colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
.copy(
containerColor = Color.Red,
titleContentColor = Color.White,
navigationIconContentColor = Color.White,
actionIconContentColor = Color.White,
),
title = {
Text("Compose Lesson")
},
navigationIcon = {
IconButton(
onClick = {
Toast.makeText(
this@MainActivity,
"Close",
Toast.LENGTH_SHORT
).show()
}
) {
Icon(
Icons.Default.Close,
contentDescription = "Close",
)
}
},
actions = {
BadgedBox(
badge = {
Badge { Text("99") }
}
) {
IconButton(
onClick = {
}
) {
Icon(
Icons.Default.Notifications,
contentDescription = "Notification",
)
}
}
Text(
"Setting",
color = Color.White,
)
}
)
},
bottomBar = {
Box(
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Red)
)
},
) {
Box(modifier = Modifier.padding(it)) {
MyButtons()
}
}
}
}
}
//color shape border
@Composable
fun MyButtons() {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = {
Toast.makeText(this@MainActivity, "Filled Button", Toast.LENGTH_SHORT).show()
},
) {
Row {
Text("Share")
Spacer(modifier = Modifier.width(4.dp))
Icon(Icons.Default.Share,
contentDescription = "Share")
}
}
FilledTonalButton(
onClick = {
Toast.makeText(this@MainActivity, "Filled Tonal Button", Toast.LENGTH_SHORT).show()
},
colors = ButtonDefaults.filledTonalButtonColors(),
shape = RoundedCornerShape(8.dp)
) {
Text("Filled Tonal Button")
}
OutlinedButton(
onClick = {
Toast.makeText(this@MainActivity, "Outlined Button", Toast.LENGTH_SHORT).show()
},
border = BorderStroke(1.dp,Color.Red)
) {
Text("Outline Button")
}
ElevatedButton(
onClick = {
Toast.makeText(this@MainActivity, "Elevated Button", Toast.LENGTH_SHORT).show()
},
elevation = ButtonDefaults.elevatedButtonElevation(
defaultElevation = 20.dp,
)
) {
Text("Elevated Button")
}
TextButton(
onClick = {
Toast.makeText(this@MainActivity, "TextButton Button", Toast.LENGTH_SHORT).show()
}
) {
Text("TextButton Button")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment