Colours can be specified in a variety of ways, including:
Named colours via the Color class, e.g. Color.Yellow (limited range)
Decimal RGB (0-255 for each value), e.g. Color(255,0,255)
Decimal RGBA (RGB with alpha channel, 0-255 for each value), e.g. Color(255,0,0,150)
Hex ARGB (RGB with alpha channel, format 0xAARRGGBB), e.g. Color(0xff336699)
Background Colour
Hello World!
Colour can't simply be applied to the Window background. Instead colour is applied to the Row and/or Column containers within using Modifiers.
funmain()=singleWindowApplication(
title ="Coloured Window"){App()}@ComposablefunApp(){Column(
modifier = Modifier.fillMaxSize().background(Color.Cyan)){Text("Hello World!")}}
Text Colour
Hello WorldfromKotlin Compose MP!
Colour can be applied to any text by setting the color parameter to the desired colour.
funmain()=singleWindowApplication(
title ="Coloured Text"){App()}@ComposablefunApp(){Column(){Text(
text ="Hello World",
color = Color.Red
)Text(
text ="from",
color = Color.Green
)Text(
text ="Kotlin Compose MP!",
color = Color.Blue
)}}
Button Colour
Colours can be applied to buttons by setting a backgroundColor and contentColor value (see the example code).
The default font for all UI elements has been altered to this monstrosity... Don't ever do this!This text has a different font specified
Fonts can be loaded and used, either as the default font for the whole UI, or on an individual text element.
Font files should be placed into a 'fonts' folder within the resources folder inside your project (see here). TrueType (TTF) fonts should be used.
Note, make sure the correct Font class is imported: androidx.compose.ui.text.platform.Font
val antonFont =FontFamily(Font("fonts/Anton-Regular.ttf"))val caveatFont =FontFamily(Font("fonts/Caveat-Regular.ttf"))funmain()=singleWindowApplication(
title ="Fonts"){App()}@ComposablefunApp(){// Set the default font for the appMaterialTheme(
typography =Typography(defaultFontFamily = caveatFont)){Column(){// All elements will use default font...Text("The default font for all UI elements ...")Row(){OutlinedTextField(
label ={Text("Name")},
value ="Jimmy Smith",
onValueChange ={...})Button(onClick ={}){Text("Press Me!")}}// ... unless a different font is specifiedText("This has a different font specified",
fontFamily = antonFont
)}}}
Themed Example 1
My Important NotesOrganise a get-together with the girlsMeet Jimmy for lunchContact the bank about my lost BitcoinsBank password: ThisIsMySecret123Need to think of a good way to get rich
This example note-taking app shows off various theming techniques: custom fonts; background, button and text colours; custom borders; etc.
Fenrick GreenleafDruidAble to call on the elemental forces of nature and can emulate the creatures of the animal world. An embodiment of nature's resilience, cunning, and fury.STR10DEX17CON9INT12WIS18CHA11
This example game window showing some player stats, shows off various theming techniques: custom font, background and text colours, borders, etc.