Set Button text color – Android Jetpack Compose

Set Button text color – Android Jetpack Compose

To set text color for Button in Android Jetpack Compose, you can set the colors parameter with the required ButtonColors instance. We can create a ButtonColors instance using ButtonDefaults.buttonColors(). To this buttonColors(), specify the contentColor parameter with the required Color value.

Set Button text color - Android Jetpack Compose

A sample code snippet to set a specific RGB text color for the Button composable is given below.

Button(
    onClick = { },
    colors = ButtonDefaults.buttonColors(
        //set with required Color value
        contentColor = Color(red = 255, green = 100, blue = 100)
    )
) {
    Text(text = "Click Me")
}

Example Android application with Button text color

Let us display a Button in the Android UI, using Button composable. Set the Button’s text or content color to Color.Black.

MainActivity.kt

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 20.dp),
                    horizontalAlignment = Alignment.CenterHorizontally) {
                    Button(
                        onClick = { },
                        colors = ButtonDefaults.buttonColors(
                            //set with required Color value
                            contentColor = Color.Black
                        )
                    ) {
                        Text(text = "Click Me")
                    }
                }
            }
        }
    }
}

Screenshot

Set Button text color to Yellow

Let us play with some other colors or Button’s content or text.

We can also set a custom RGB color for the Button’s content or text using Color().

Button(
    onClick = { },
    colors = ButtonDefaults.buttonColors(
        //set with required Color value
        contentColor = Color(red = 255, green = 100, blue = 100)
    )
) {
    Text(text = "Click Me")
}
Set Button text color to RGB - Android Jetpack Compose