Search code examples
androidandroid-jetpack-composebanner

what is equivalent to banner in android (jetpack compose)


I am trying to implement a banner which looks like this in android enter image description here

I found this at Material Design but it doesn't have implementation for android.

what will be the best way to implement this in android especially with Jetpack Compose?

thank you in advance.


Solution

  • There is no Banner composable in version 1.0.3.

    Creating a custom composable for a banner is quite easy.

    Example code

    Custom Banner

    @Composable
    fun Banner(
        text: String,
        button1Text: String? = null,
        button2Text: String? = null,
        button1ClickListener: (() -> Unit)? = null,
        button2ClickListener: (() -> Unit)? = null,
    ) {
        Column {
            Text(
                text = text,
                modifier = Modifier.padding(16.dp),
            )
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(
                        start = 8.dp,
                        end = 8.dp,
                        bottom = 8.dp,
                    ),
                horizontalArrangement = Arrangement.End,
            ) {
                button1Text?.let {
                    TextButton(
                        onClick = if (button1ClickListener != null) {
                            button1ClickListener
                        } else {
                            {}
                        }
                    ) {
                        Text(
                            text = button1Text,
                        )
                    }
                }
                button2Text?.let {
                    TextButton(
                        onClick = if (button2ClickListener != null) {
                            button2ClickListener
                        } else {
                            {}
                        }
                    ) {
                        Text(
                            text = button2Text,
                        )
                    }
                }
            }
            Divider()
        }
    }
    

    Usage

    @Composable
    fun BannerUsage() {
        Banner(
            text = "There was a problem processing a transaction on your credit card.",
            button1Text = "FIX IT",
            button2Text = "LEARN MORE",
        )
    }
    

    Screenshot

    Custom Banner