I am trying to customize the Typography in my project. But it not affected in my widgets.
My TypoGraphy customization:
Code:
subtitle1 = TextStyle(
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold,
fontSize = 30.sp,
)
In my Text :
Text(text = "Homemade veg pizza", style = MaterialTheme.typography.subtitle1)
My Output :
My problem:
Fontsize and fontfamily not changed. It looks same like default text. How to set custom TypoGraphy in Jetpack Compose?
It's my mistake. I figure out.
We need to set the MaterialTheme before use custom TypoGraphy.
In my rootView I place my rootView under the MaterialTheme. Issue fixed.
setContent {
MaterialTheme { //I missed this line
ThemesRootView()
}
}
Output:
Related
I discovered today that MaterialTheme applies an alpha to Text's colour. As you can see from the example attached, when I change the background colour, the text's colour appears to be different because it has a transparency value. I can force set a colour (Text(color = MaterialTheme.colors.onBackground, ....)) and this works correctly but I don't want to have to do this for every single Text.
Why does MaterialTheme do this? How do I override this behaviour?
Compose and Material Compose Material version: 1.2.1
#Preview
#Composable
private fun Preview_Playground() {
MaterialTheme {
Box(Modifier.background(Color.Green)) {
Text("Test", fontWeight = FontWeight.ExtraBold, modifier = Modifier.alpha(1f))
}
}
}
With M2 (androidx.compose.material) the color of the Text is defined by the color parameter or applying a TextStyle.
The default value is Color.Unspecified.
If color = Color.Unspecified and style has no color set, this will be LocalContentColor mixed with LocalContentAlpha.current.
In the Text.kt you can find:
val textColor = color.takeOrElse {
style.color.takeOrElse {
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
}
}
With M3 (androidx.compose.material3) it doesn't happen since LocalContentColor.current is not mixed:
val textColor = color.takeOrElse {
style.color.takeOrElse {
LocalContentColor.current
}
}
If you have to use M2, you can define a custom composable for your Text, or you can change the LocalContentAlpha in the theme for the whole application (not only the Text):
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes
){
CompositionLocalProvider(LocalContentAlpha provides 0.5f) {
content()
}
}
I know I can use Code A to create a instance of TextStyle with color, fontFamily and etc.
Now I hope to get a instance of TextStyle based MaterialTheme.typography.body2, and I hope that the style has customized color Red.
How can I do? BTW, Code B is wrong.
Code A
var style = TextStyle(
color = Color.Red
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 14.sp
)
Code B
var style: TextStyle = MaterialTheme.typography.body2
style.color= Color.Red
you can use TextStyle.copy
Like this
var style = MaterialTheme.typography.body2.copy(color = Color.Red)
how can i change the opacity of Text Color.White in JetPack Compose
Text(text = funfact , fontSize = 18.sp, color = Color.White )
You can change the alpha channel from the Color attribute:
Text(text = funfact, fontSize = 18.sp, color = Color.White.copy(alpha = 0.5f))
You can also user the alpha modifier :
Text(
text = funfact,
modifier = Modifier.alpha(0.5f),
fontSize = 18.sp,
color = Color.White,
)
Insert the text component inside a CompositionLocalProvider as follows:
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Text(text = "Your text")
}
ContentAlpha contains 3 default types of opacity: medium, high and disabled.
Tested on stable version of Compose 1.3.1.
I'm having problems with Japanese font types. Specifically with the font Noto Sans JP. When I apply the font to Text, I see that the vertical padding of the font seems too wide.
Here is my text display code:
Text(
text = "地域のお得は\nすべてここに",
style = Typography.h4,
)
// Typography
val Typography = Typography(
...
h4 = TextStyle(
fontFamily = NotoSans,
fontWeight = FontWeight.Normal,
fontSize = 34.sp,
letterSpacing = 0.25.sp
),
...
)
// Import font
private val NotoSans = FontFamily(
Font(R.font.noto_sans_black, FontWeight.Black),
Font(R.font.noto_sans_light, FontWeight.Light),
Font(R.font.noto_sans_bold, FontWeight.Bold),
Font(R.font.noto_sans_thin, FontWeight.Thin),
Font(R.font.noto_sans_medium, FontWeight.Medium),
Font(R.font.noto_sans_regular, FontWeight.Normal),
)
Link font: Noto Sans JP
I want to remove vertical padding of Text. With Android Baseview, there is includeFontPadding = false attribute to remove font padding. But with Android Compose I can't find any properties with similar functionality.
So in Android Compose, is there a way to remove the vertical padding of the font?
I found the solution for my question. By using Compose 1.2.0-alpha07 and above, you can use PlatformTextStyle api to set includeFontPadding.
Try to the below code:
private val NotoSans = FontFamily(
Font(R.font.noto_san_jp_black, FontWeight.Black),
Font(R.font.noto_san_jp_light, FontWeight.Light),
Font(R.font.noto_san_jp_bold, FontWeight.Bold),
Font(R.font.noto_san_jp_thin, FontWeight.Thin),
Font(R.font.noto_san_jp_medium, FontWeight.Medium),
Font(R.font.noto_san_jp_regular, FontWeight.Normal),
)
val Typography = Typography(
headlineLarge = Typography().headlineLarge.copy(
fontFamily = NotoSans,
)
)
#OptIn(ExperimentalTextApi::class)
/* ... */
Text(
text = "地域のお得は\nすべてここに",
style = MaterialTheme.typography.headlineLarge.copy(
platformStyle = PlatformTextStyle(
includeFontPadding = false
)
/* ... */
)
)
The result when includeFontPadding = false:
The result when includeFontPadding = true or no using it:
More information:
Fixing Font Padding in Compose Text - Medium
The Japanese font Noto Sans JP does show an unusual amount of extra white-space or gap above the first line of text when displayed on a screen:
However, this gap can be reduced by including a negative padding offset in the Text() composable - see 'modifier' parameter below:
#Composable
fun Greeting() {
Text(
text = "地域のお得は\nすべてここに",
fontFamily = NotoSans,
style = Typography.h4,
textAlign = TextAlign.Center,
modifier = Modifier
.padding(top = 0.dp).offset(y = (-30).dp)
)
}
This results in the same text being displayed, but without the gap:
If you would like to adjust the line spacing between the lines then you can add the "lineHeight" parameter to either the TextStyle() composable in the 'Type.kt' file or the Text() composable in the 'MainActivity.kt' file, as required; e.g. in this instance "lineHeight = 80.sp":
Hope this is helpful to someone!
How do I reference a resource style from Compose widget?
styles.xml
<style name="Style.Monet.TextView.HeaderSubtext" parent="Widget.AppCompat.TextView">
<item name="android:textColor">#737373</item>
<item name="android:textSize">12sp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginBottom">16dp</item>
<item name="android:layout_marginEnd">24dp</item>
</style>
MyComponent.kt
Text(text = "June 2021", style = TextStyle(R.style.Style_TextView_HeaderSubtext))
But this doesn't work. Is there a way to make this work?
You can't do that, because Compose Text is styled differently, and TextStyle it not responsible so all xml style is responsible. As an example, you cannot add margins.
You can create compose TextStyle:
val textStyle = TextStyle(
color = Color(0xFF737373),
fontSize = 12.sp,
)
And use it globally in your project or pass to your theme. This is a preferred way to use styles in compose, check out more about it in the theming documentation. Customize one of material available styles:
val typography = Typography(
body1 = TextStyle(
color = Color(0xFF737373),
fontSize = 12.sp,
)
)
Pass it to your theme:
#Composable
fun ComposePlaygroundTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: #Composable () -> Unit
) {
val colors = if (darkTheme) {
DarkThemeColors
} else {
LightThemeColors
}
MaterialTheme(
colors = colors,
typography = typography,
shapes = shapes,
content = content,
)
}
Apply Theme at the composable root:
setContent {
ComposePlaygroundTheme {
// your composables
}
}
After that you can use it like this:
Text("",
style = MaterialTheme.typography.body1,
)
To apply margins in compose you need to use padding modifier. Check out more about layout in compose in the layout documentation:
If you wanna reuse same styled text in compose, you can create your own composable with predefined style and padding:
#Composable
fun ProjectText(text: String, modifier: Modifier) {
// without material theme you can just define text style here and pass to text
// val textStyle = TextStyle(
// color = Color(0xFF737373),
// fontSize = 12.sp,
)
Text("",
style = MaterialTheme.typography.body1,
modifier = modifier
.padding(start = 16.dp, end = 24.dp, bottom = 16.dp)
)
}
Usage:
ProjectText("June 2021")