I'm trying to remove the padding at the bottom and top of the text, but I haven't been able to remove the padding successfully, any suggestions?
Text(
text = "DT",
color = androidx.compose.ui.graphics.Color.Black,
style = androidx.compose.ui.text.TextStyle(
fontFamily = poppinsFamily,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center
),
fontSize = 54.sp,
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 0.dp),
)
You can use PlatformTextStyle attribute to set includeFontPadding = false
Example:
Text(
text = text,
style = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = false
)
)
)
Details:
https://android-developers.googleblog.com/2022/05/whats-new-in-jetpack-compose.html
Related
I am trying to create a similar-to-scrollview behavior with Jetpack Compose, which should be simple but I find it not working (I can't scroll the Column). I have been following the example from here:
https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/scrollers/VerticalScrollableActivity.kt#L104
And this is my code:
#Composable
fun MainScreen() {
val scrollState = rememberScrollState()
Column(
modifier = Modifier
.fillMaxSize()
.scrollable(
state = scrollState,
orientation = Orientation.Vertical
)
) {
Text(
text = "This is my first Compose App",
fontSize = 24.sp,
fontWeight = FontWeight.Light,
lineHeight = 40.sp,
color = Color.Black,
modifier = Modifier.padding(top = 32.dp, start = 24.dp)
)
Row(
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFF6A0DAD))
.height(2200.dp))
{}
}
}
as mentioned #bylazy using Modifier.verticalScroll(scrollState) solves it
This question already has answers here:
How to avoid tinting Icon with painterResource().It paints my vector in Black
(2 answers)
Closed 7 months ago.
I have the following composable
#Composable
fun GoogleLoginButton(onLoginClicked: () -> Unit) {
IconButton(
modifier = Modifier
.fillMaxWidth()
.background(color = Color.White, shape = RoundedCornerShape(10.dp)),
onClick = {
onLoginClicked()
},
) {
Box(modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center) {
Icon(
modifier = Modifier.padding(start = 12.dp).align(Alignment.CenterStart),
painter = painterResource(id = R.drawable.ic_google_logo),
contentDescription = "Google logo"
)
Text(modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
fontSize = 18.sp,
text = "Google", color = Color.Black)
}
}
}
It displays the correct logo for for some reason its displayed as black icon.
However, the actual colors of the vector image is this:
Icon has a tint argument which is set to LocalContentColor by default. Either set it to Color.Unspecified, or use Image composable instead.
I am working on this design where I have a column that has text and image on top and I have this card view but I want to remove this white space. The card is white I just added the blue to show the white space showing. How can I remove the white space. I want the R.color.purple_700 to fill that space.
Here is my code
Column(
modifier = modifier
.fillMaxSize()
) {
Column(
modifier = modifier
.fillMaxWidth()
.background(colorResource(id = R.color.purple_700))
.weight(2.0f),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// Do I add the box here?
Image(
painter = painterResource(id = R.drawable.ic_launcher_background),
contentDescription = "Logo"
)
Text(
text = "Sign in to Continue",
color = (colorResource(id = R.color.white)),
fontSize = 28.sp,
modifier = Modifier.padding(8.dp)
)
}
Card(
shape = RoundedCornerShape(topStart = 32.dp, topEnd = 32.dp),
modifier = modifier
.fillMaxWidth()
.weight(5.0f), elevation = 8.dp
) {
Column(
modifier = modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
.scrollable(scrollState, Orientation.Vertical),
horizontalAlignment = Alignment.CenterHorizontally
) {
// then some outlinedText
}
Just add the background modifier to the Card.
Card(
modifier = Modifier.background(colorResource(id = R.color.purple_700))
//....
)
In jetpack Compose, you can Justify a Text like this:
Text(
text = text,
textAlign = TextAlign.Justify
)
If you want support RTL, you achive this by:
Text(
text = text,
textAlign = TextAlign.Right
)
How can a Text() support RTL text and justify it same time in Jetpack Compose?
After several hour testing, I reach this:
#Composable
fun JustifiedRTLText(
text: String,
modifier: Modifier = Modifier
) {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
Text(
text = text,
textAlign = TextAlign.Justify,
modifier = modifier,
)
}
}
In Jetpack Compose RTL or LTR will automatically set by according to the text content. But we can force it by changing the textDirection of style to TextDirection.Content or TextDirection.RTL
Text(
text = text,
textAlign = TextAlign.Justify,
style = TextStyle(textDirection = TextDirection.Content)
)
#Composable fun Gradientcard() {
val horizontalGradientBrush = Brush.horizontalGradient(
colors = listOf(
Blue,
lightBlue
)
)
Card(modifier = modifier = Modifier
.background(brush = horizontalGradientBrush),shape = RoundedCornerShape(20.dp)){
Text(
text = "sub 1",
)
}
This method made background of card as gradient but not the card. Card color is still white.
Output
Instead of using modifier in Card try creating Box layout inside Card and add gradient code inside it.
Card(
modifier = Modifier
.fillMaxWidth()
.height(175.dp),
elevation = 4.dp,
shape = RoundedCornerShape(24.dp),
) {
Box(
Modifier
.background(
/* Your code*/ ) {
Text(
text = "Card Gradient Background",
)
}
}
Just cut that modifier from theText, and paste it in the Card
Card(modifier = ... /*Paste*/){
}