Primary Button

Preview

Logo

Description

The Primary Button component is a key user interface element designed to emphasize primary actions within an application. It typically stands out visually with a distinct color or style compared to secondary buttons. The Primary Button is often used for critical actions like submitting forms, confirming decisions, or initiating important processes.

Characterized by a prominent appearance, the Primary Button is designed to catch the user's attention and encourage interaction. It usually features a bold color scheme that aligns with the application's branding or design language, making it easily recognizable as the primary action button.

The Primary Button component plays a crucial role in enhancing user experience by guiding users towards essential functionalities and promoting engagement with key features of the application. Its design and placement are carefully considered to ensure that users can quickly identify and interact with primary actions, contributing to a seamless and intuitive user interface.

Sample Code

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun PrimaryButton(
    modifier: Modifier = Modifier,
    text: String,
    isEnable: Boolean = true,
    onClick: () -> Unit,
) {
    Button(
        text = text,
        textStyle = MaterialTheme.typography.labelLarge.copy(color = Color.White),
        backgroundColor = Color(0xFFD10369),
        isEnable = isEnable,
        modifier = modifier,
        onClick = {
            onClick.invoke()
        }
    )
}

@Preview
@Composable
fun PreviewPrimaryButton() {
    PrimaryButton(
        text = "Button",
        isEnable = true,
        onClick = {}
    )
}

Component Code

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun Button(
    modifier: Modifier = Modifier,
    text: String,
    textStyle: TextStyle = MaterialTheme.typography.labelLarge.copy(color = Color.Black),
    backgroundColor: Color = Color.White,
    backgroundColorDisable: Color = Color.Gray,
    isEnable: Boolean = true,
    borderColor: Color? = null,
    borderColorDisable: Color? = null,
    borderSize: Dp = 1.dp,
    roundedCorner: Dp = 8.dp,
    @DrawableRes iconLeft: Int? = null,
    onClick: () -> Unit,
) {
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(roundedCorner))
            .background(if (isEnable) backgroundColor else backgroundColorDisable)
            .clickable(onClick = {
                if (isEnable) onClick.invoke()
            })
            .border(width = borderSize, color = if (isEnable) (borderColor ?: backgroundColor) else borderColorDisable ?: backgroundColorDisable, shape = RoundedCornerShape(roundedCorner))
    ) {
        iconLeft?.let {
            Image(
                painter = painterResource(id = it),
                contentDescription = text,
                modifier = Modifier.padding(20.dp).size(20.dp).align(Alignment.CenterStart)
            )
        }

        Text(
            text = text,
            style = textStyle,
            textAlign = TextAlign.Center,
            modifier = Modifier.fillMaxWidth().padding(horizontal = 24.dp, vertical = 16.dp)
        )
    }
}

Parameters

The Primary Button component is a key user interface element designed to emphasize primary actions within an application. It typically stands out visually with a distinct color or style compared to secondary buttons. The Primary Button is often used for critical actions like submitting forms, confirming decisions, or initiating important processes.

Characterized by a prominent appearance, the Primary Button is designed to catch the user's attention and encourage interaction. It usually features a bold color scheme that aligns with the application's branding or design language, making it easily recognizable as the primary action button.

The Primary Button component plays a crucial role in enhancing user experience by guiding users towards essential functionalities and promoting engagement with key features of the application. Its design and placement are carefully considered to ensure that users can quickly identify and interact with primary actions, contributing to a seamless and intuitive user interface.

Becompose

Professionally crafted Jetpack Compose UI components & resources.

Job Board
  • Browse Jobs
  • Post Android Job

Let's explore together

Perfect for those using Android Views and want to move to Jetpack Compose.

Social Media

Logo
Logo
Logo

Becompose - ©2024