How to do muted / grayed text in jetpack compose? - android-jetpack-compose

This seems to exist in most UI frameworks, in a way that will work with both light and dark themes, but it seems to be absent here.
Currently I've resorted to doing:
val Muted = Color.LightGray
and importing that elsewhere, but it looks bad on light themes.
Any ideas?

Compose has ContentAlpha.medium, that is used, as the name suggests, to display a color as muted.
I don't know if this is the most efficient way to do it, but I usually use it like this:
Text(
text = myText,
color = when {
isEnabled -> MaterialTheme.colors.myColor
else -> MaterialTheme.colors.myColor.copy(alpha = ContentAlpha.medium)
}
)
You could abstract it into a extension field and add it to your themes file:
val Color.muted get() = this.copy(alpha = ContentAlpha.medium)
And use it like this:
color = when {
isEnabled -> MaterialTheme.colors.myColor
else -> MaterialTheme.colors.myColor.muted

Using Color.luminance() value of the background color and choose the Grayscale color of the text in case of dynamic theming.
Using isSystemInDarkTheme() to pick color in combination with contentColorFor(), when we have fixed theming system,

Related

How to animate a background to a target value and back in Android Compose

I'm probably missing something very obvious, but anyway. Say, I want to animate a background color like this: Grey -> Red -> Grey. However the function animateColorAsState does not seem to allow such a transition. What is the canonical way in Android Compose to make it happen?
Here's how you can do it with Animatable:
val color = remember { Animatable(Color.Gray) }
LaunchedEffect(Unit) {
color.animateTo(Color.Red, animationSpec = tween(1000))
color.animateTo(Color.Gray, animationSpec = tween(1000))
}
Box(Modifier.fillMaxSize().background(color.value))

How to set an icon's color to [default color] in jetpack compose?

An icon has a default color, and it could be different under different themes.
When meet the condition, I hope the icon's color become Color.RED, otherwise it used the default color (example, white on one theme and yellow on another theme)
But I don't know the grammar of how to set to default color. Please help, thanks a lot!
val judge = ... //a mutableStateOf Boolean
...
// how to set [default color]?
Icon(painterResource(R.drawable.ic_baseline_error_outline_24),
contentDescription = null,
tint = if (judge) Color.RED else [default color])
If you go to Icon file you see its value:
tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
You can just set it again

What is the difference between the different options under PointerIconDefaults in jetpack compose

Column(
Modifier.padding(top = 300.dp).pointerHoverIcon(PointerIconDefaults.Text)) {
SelectionContainer {
Column {
Text("Selectable text")
Text(
modifier = Modifier.pointerHoverIcon(PointerIconDefaults.Hand, true),
text = "Selectable text with hand"
)
}
}
Text("Just text with global pointerIcon")
}
PointerIconDefaults.Text:
PointerIconDefaults.Hand:
I don't see any difference between PointerIconDefaults.Hand and PointerIconDefaults.Text
Not sure what you expect from using this modifier since your image shows a floating toolbar. If you need to change how floating toolbar looks like, check out this answer. If you need to change the selection color, check out this one.
Modifier.pointerHoverIcon change the appearance of the mouse pointer over the item. To see this, for example you can connect a BLE mouse to your Android device, not sure if this can be checked in the Emulator.
Any Text has PointerIconDefaults.Text by default.

SwiftUI: Get the Dynamic Background Color (Dark Mode or Light Mode)

Is there a way to systematically access the standard, dynamic background color for SwiftUI views, regardless of whether the user be in Light or Dark Mode?
For example, I know the following can be used to get the primary (e.g. text) color:
let textColor = Color.primary
...but I don't see anything similar for getting the background color.
let backgroundColor = Color.??? // Not available
I'm looking for something that works on both iOS and macOS.
So there doesn't currently appear to be any such property built into SwiftUI's OS-independent Color class; however, UIColor and NSColor do provide accessors for it (on iOS and macOS respectively), and you can use these older color objects to initialize a SwiftUI Color object.
As a result, what you need can be achieved using a simple extension to Color, such as the below, which uses conditional compilation to work correctly on either OS.
No need to check colorScheme or userInterfaceStyle with this approach: The OS will switch automatically when the user moves between Light & Dark mode.
I've also included 'secondary' & 'tertiary' colors, which are a little subjective on macOS, but you can always change them to some of the other NSColor properties if you want.
Swift v5.2:
import SwiftUI
public extension Color {
#if os(macOS)
static let background = Color(NSColor.windowBackgroundColor)
static let secondaryBackground = Color(NSColor.underPageBackgroundColor)
static let tertiaryBackground = Color(NSColor.controlBackgroundColor)
#else
static let background = Color(UIColor.systemBackground)
static let secondaryBackground = Color(UIColor.secondarySystemBackground)
static let tertiaryBackground = Color(UIColor.tertiarySystemBackground)
#endif
}
You then simply access these from elsewhere in your code like any other SwiftUI Color. For example:
let backgroundColor = Color.background

How to change default text color of application?

How can I set text color of all Fields to white?
AFAIK, there is no such option in BB API. You can only override Field.paint(Grahpics grahpics), smth like this:
...
public void paint(Grahpics grahpics) {
int initialColor = grahpics.getColor(); // just in case
grahpics.setColor(Color.WHITE);
super.paint(grahpics);
grahpics.setColor(initialColor);
}
...
So you could create a set of custom white-colored Fields (WhiteLabelField, WhiteEditField, etc.) and use them istead of the originals.

Resources