How to learn material 3 component attribute use case - android

Can someone help me on Material 3. I found this two method darkColorScheme and lightColorScheme . When I checked the definition of this function I found so many colors used in the function. I am confused which one using where so, Is there any documentation or blog where this type of attribute is using i.e. secondaryContainer, tertiary etc. this is few example I want to learn each attribute where this is using according to their name. I am new in material design with jetpack compose.

Jetpack Compose functions darkColorScheme and lightColorScheme help you set up a whole MD3 theme with all the color roles.
You would want to set/define all colors if you want that background and foreground colors will work nicely together.
The mapping for colors can be found at
https://m3.material.io/styles/color/the-color-system/color-roles
See the sections for Mapping accent colors and for Mapping neutral colors
However, you don't have to set all colors manually. If you want you can use the theme builder, and only choose your primary, secondary, tertiary and neutral color and the theme will be generated for you (there are also extended colors if you need more). If you export it for Jetpack Compose you will get both Theme.kt and Color.kt which you then copy and replace in your project. The calls to darkColorScheme and lightColorScheme will be already included with all colors already set as parameters. If you don't like any of the mapped colors, you can then customize the theme further in code.
The theme builder https://material-foundation.github.io/material-theme-builder/#/custom
The documentation on how to use themed colors is from this section and on
https://developer.android.com/jetpack/compose/themes/material#theme-colors
Material Design 3 components (buttons, inputs, text) will by default already use themed colors based on the defined theme, light/dark modes and the surface they are on, so for common use cases you don't even need to set their colors through parameters or modifiers.
Edit: To preview various MD3 components quickly without the need to run the app you can use the #Preview annotation like this
#Preview("Light Mode")
#Preview("Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
#Composable
fun MyScreenPreview() {
NameOfYourTheme { // update this line with the name of your theme
Surface {
// place any Composable you want to preview here
Column {
Text("Preview test")
Button(onClick = {}) {
Text("OK")
}
}
}
}
}
You can place this in any file you want and then just replace the contents with various MD3 components to see how they look in the preview section both in light and dark mode with your theme.
And if you want to see their default colors, then just remove the NameOfYourTheme { } block.
For the above this is what I get for my theme in the preview section (your colors will be different of course)

Related

Can't override the TextFieldColors textColor in Jetpack Compose

I'm trying to customize the colors of a TextField of Jetpack Compose. For this I'm creating an instance of TextFieldColors where I set the textColor to one of my custom colors. But the result has been that the TextField keeps using the default colors.
I been reading that this could be modified with a CompositionLocalProvider, but then why is there an option on TextFieldColors? Also, changing it with CompositionLocal wouldn't allow for more than one custom color on my TextFields.
Some ilustrations of the problem, the first picture shows the TextField in light mode, the text can't be seen on dark mode because it doesn't change to from the default black. All the other parts of the component change corretly, since they are using my custom colors.
Is there a way to achieve this?
Also I'm using Material 2, would it make any difference to change to M3?

Jetpack Compose theming at runtime from network api call

I have an API call for theming that returns all colors for my application. I want to apply these colors all of jetpack components. can anyone please suggest a proper way to do this.
I have two files theme. kt and color. kt .
I need to change values in these classes dynamically.
you should receive your colors from API then store that in dataStore then observe your data store in the main theme compose function whenever colors changed then create a new color pallet then set that in your MaterialTheme. automatically everywhere used MaterialTheme colors are updated.

How to theme a Compose app with more colors than Material theme allows?

We have an app using Android View system that we want to start migrating to Jetpack Compose soon.
The app has some screens that look pretty different so that it doesn't seem to work to use a single Material theme for the whole app. We can't really figure out which color will be primary, secondary and variants. We have more colors than this.
So I see two alternatives: Either don't use Material theme but make own own custom theme for the whole app, or use Material theme but have two or three different themes so each screen can use the relevant one.
It seems pretty easy to for solution 2 (several Material themes in a single app) in Compose. But I'm wondering if it's a good practice, if there are cons that we haven't thought about?
Thanks for your advices!
Compose library has limited set of available colors in Colors class. If you want to have additional colors there are two solutions I can think of:
Create extensions for Colors class for each specific color. This is fast but downside of this method is that you will have to handle logic with theming inside of each color added. Sample
Create custom Colors class yourself and provide it using CompositionLocal . This way you won't need to include theme checks everywhere, you can do it only once and provide required Colors instance with CompositionLocalProvider

Are there in android vars or constants with colors of color sceme?

Other languages I used to use some constants that named something like "BackgroundColor" or "TextColor". If I want to color my component I just set its color using that constants, and I didn't care what color it was in particular.
For example, I set some color to ColorText and when user changed main color sceme my component will be visible for sure and color will suit this color sceme. And if I will set my color to just Black there may be a circumstance when it just will not be visible.
Is there a color-constants that change inside for different color scemes in android?
No, there's no one BackgroundColor for all etc. But you can get the same effect by using styles and themes. Please developer docs here and example tutorial here

Consistent UI color in all Android devices

I noticed the UI color (eg Button background/text color) all changes from device to device, based on the current theme that is being used in a device.
What is the best practice to apply custom UI colors for Android app, so that I have same color scheme for my app in all Android devices. I can set text/background color on a UI item. I'm wondering if there is a single place where I can define all the colors which will override the current theme applied on the phone.
thx.
Yes, there is a single place where you can define these values for your app. See Styles and Themes in the Android docs for how it works.
A style is just a mapping of values to predefined names. If you find yourself repeating a number of common attributes in your layouts, you can factor that out into a style. For example, you might have a special button style that defines a specific background and text color.
A theme is a sort of meta-style. It can be applied to an Activity or even a whole application through your AndroidManifest.xml. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.
When you're trying to blend in with the system in an otherwise custom UI for your app, you can query the current theme for values. Just like you use the # reference syntax #android:drawable/foo when referring to a system resource, you can use the syntax ?android:attr/foo when you want to use the value stored in the system theme attribute foo.
In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary. If you just want to be sure that an element of your app is using the primary text color as defined by the device your app is running on, you can set android:textColor="?android:attr/textColorPrimary". The same principles apply elsewhere as well.
If you want to see what attributes are used in the system, they are defined as part of the Android framework in this file: frameworks/base/core/res/res/values/attrs.xml. Look at the children of the XML element <declare-styleable name="Theme"> at the top. To see examples of what the system sets these to, see themes.xml in the same directory. Finally, not all of these attributes are public - non-public attributes cannot be set by an app, they're implementation details of the Android framework. See public.xml for the complete list of which attributes are available for use in apps.
Best practice is to apply a custom theme to your application, and override as much of the default properties as you need.
Almost everything can be changed, except
The Menu
Some properties of AlertDialog (these can be changed using a custom dialog)
OS provided views such as the Quick Search Bar (QSB)
If you like the look of the default SDK resources then you can find these in sdk_folder/platforms/android-9/data/res/ (replace 9 with the SDK version you want the resources from) - copy the ones you want into your App and reference those.
You can take a look at the theme the SDK uses:
themes.xml
styles.xml

Categories

Resources