How to add Button with Transparent Background in Android Jetpack Compose

By Mohammed Rashid •  November 21st, 2022 • 

Buttons are one of the most important UI components for mobile app development. Buttons always have solid colors as the background. But sometimes, developers want to have a transparent background. In this Jetpack Compose tutorial, let’s learn how to show a button with transparent background.

We have to change the backgroundColor to make the Button transparent. Compose already has a color named transparent and we can use it for the desired output.

@Composable
fun TransparentButtonExample() {
    Box() {
        Button(onClick = { println("Button Clicked!") },
            modifier = Modifier.align(Alignment.Center),
            colors = ButtonDefaults.buttonColors(backgroundColor = Color.Transparent)) {
            Text(text = "Transparent Button")
        }
    }
}

You will get the following output.

jetpack compose transparent button

Following is the complete code for reference.

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.Alignment
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    color = MaterialTheme.colors.background,
                    modifier = Modifier.fillMaxSize()
                ) {
                    TransparentButtonExample()
                }
            }
        }
    }
}

@Composable
fun TransparentButtonExample() {
    Box() {
        Button(onClick = { println("Button Clicked!") },
            modifier = Modifier.align(Alignment.Center),
            colors = ButtonDefaults.buttonColors(backgroundColor = Color.Transparent)) {
            Text(text = "Transparent Button")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        TransparentButtonExample()
    }
}

That’s how you add a transparent button in Jetpack Compose.

Keep Reading