MaterialComponents typography opacity - android

In MaterialComponents theming we can use attributes to either use the default styles or extend them to reflect your design guidelines, e.g.:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="textAppearanceCaption">#style/TextCaption</item>
...
</style>
<style name="TextCaption" parent="TextAppearance.MaterialComponents.Caption">
<item name="fontFamily">#font/inter_regular</item>
</style>
Then the framework automatically applies opacity to the text colors based on the theme, which is kinda cool, because you don't need to handle it manually. For example as far as I know the normal texts in light theme are getting 87%, the caption is getting somewhere around 40%.
But what if I'd like e.g. make the textAppearanceCaption have the same opacity as body1 app-wide without setting the color directly? Is something like this possible? How and when is this opacity applied by the framework? Is it the part of styles?

Related

Applying ThemeOverlay for custom theming a MaterialAlertDialogBuilder dialog

My app theme is set up like this:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorOnSurface">#color/appColorOnSurface</item>
...
</style>
But when I use MaterialAlertDialogBuilder the text contrast is very poor (because material dialog uses colorOnSurface with 60% alpha, instead of textColorPrimary). So I tried to use this ThemeOverlay:
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.Dialog">
<item name="colorOnSurface">#color/appColorOnSurfaceHighContrast</item>
</style>
and applying it like this:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="materialAlertDialogTheme">#style/ThemeOverlay.App.Dialog.HighContrast</item>
<item name="colorOnSurface">#color/appColorOnSurface</item>
...
</style>
However, this causes problems when displaying a list of items in the dialog. Each item touch area is limited to the area of text being displayed instead of stretching the width of the dialog like normal.
Furthermore, the theme does not appear to be Material, but rather AppCompat style.
Why does the ThemeOverlay approach cause the unexpected touch area (as if WRAP_CONTENT) issue? Is that not the correct way to apply a ThemeOverlay? Or is there another way to get the alert dialog to use #color/appColorOnSurfaceHighContrast?
I fixed this by using ThemeOverlay.MaterialComponents.MaterialAlertDialog instead of ThemeOverlay.MaterialComponents.Dialog.Alert, and also using materialAlertDialogBodyTextStyle to make sure only the dialog text body was being styled:
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogBodyTextStyle">#style/MaterialAlertDialog.App.Body.Text.HighContrast</item>
</style>
<style name="MaterialAlertDialog.App.Body.Text.HighContrast" parent="#style/MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">#color/appColorOnSurfaceHighContrast</item>
</style>
But why does AndroidStudio auto-complete only show ThemeOverlay.MaterialComponents.Dialog.Alert and not ThemeOverlay.MaterialComponents.MaterialAlertDialog?
Note: there were actually two issues going on here:
Changing the parent ThemeOverlay from ThemeOverlay.MaterialComponents.Dialog to ThemeOverlay.MaterialComponents.Dialog.Alert fixed the touch area issue, but I was still getting an AppCompat (non-Material) theme.
Changing the parent ThemeOverlay from ThemeOverlay.MaterialComponents.Dialog.Alert to ThemeOverlay.MaterialComponents.MaterialAlertDialog fixed the theme to make it appear Material.
UPDATE: It looks like ThemeOverlay.MaterialComponents.Dialog.Alert is for use with alertDialogTheme and ThemeOverlay.MaterialComponents.MaterialAlertDialog is for use with materialAlertDialogTheme. See: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/dialog/res/values/themes.xml#L60
However, that still doesn't explain why the latter doesn't auto-complete.

Use Night resources on Light theme

I'm using the Material Component's dark theme style, everything is working fine but now I need to show a particular view with a different theme to obtain more contrast.
I've defined my main theme in values/themes.xml like that:
<style name="Base.AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/color_primary</item>
<item name="colorPrimaryVariant">#color/color_primary_variant</item>
<item name="colorOnPrimary">#color/color_on_primary</item>
...
</style>
and two different colors resources in values/colors.xml and values-night/colors.xml.
What I would like to do is to find a way to retrieve the light theme colors when the dark theme is used (and vice-versa) or to apply the opposite theme to a single view.
The only solution I found was to simply define two variants (normal and "reversed") for each color in both colors.xml files, but since there are many colors defined I would like to avoid that.
To avoid having to define multiple light/dark values variant for each color, I defined a custom style that refers to the opposite theme variant, e.g. in values/styles.xml I defined:
<style name="ThemeOverlay.AppTheme.Reverse" parent="ThemeOverlay.MaterialComponents.Dark"/>
and in values-night/styles.xml:
<style name="ThemeOverlay.AppTheme.Reverse" parent="ThemeOverlay.MaterialComponents.Light"/>
Then in the root of layout where I want to use the "reversed" theme:
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppTheme.Reverse">
...
</LinearLayout>

Defining styles in Android and need to be inheriting from a parent

I am a little confused about how I should be writing my styles.
I have written some styles and they appear to work great but I am unsure if I should be inheriting from a style.
For example, by default, a text view (for example) has a default style before applying mine?
So I should be inheriting from something else in my style before applying it, i.e. a halo style?
So, for example, I designed the following style
<style name="TestMe">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFF0000</item>
</style>
Which I have applied to a few text views, seems to work great but should I be doing
<style name="TestMe" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFF0000</item>
</style>
Inheriting from a parent, if so which one?
If I apply a style to my text view and do not inherit, in effect is the text view losing a lot of styles that were predefined on it before applying my style? I know that I have an app theme that inherits from a parent and this is applied in the androidmanifest.xml. So adding a style doesn't override the theme, which in essence is a style?
Or is inheritance on styles only being used when I want to override something?
Be aware of the precedence order of different styling techniques — if you’re trying to style some text and not seeing the results you expect then your changes are likely being overridden by something higher up in this hierarchy:
Here the whole detail regarding text appearance URL

Android add custom attribute to styles

I'm trying to implement a feature to let the user change the theme.
I've done this but depending on if the theme is light or dark, I need to use the opposite color to draw line on a canvas. I thought the best way to do this would be to simply call R.style.colorAttribute to get the correct color when doing the drawing.
In the styles.xml I have defined the following themes:
<style name="DarkTheme" parent="android:Theme.Holo" />
<style name="LightTheme" parent="android:Theme.Holo.Light" />
Can anyone help on the best way to add an attribute here which will store the color. I've not done this before and was n't sure if I should use the color.xml file or the styles.xml file.
Thanks
And just to make it clear
I for the dark theme I need a white color
and for the light them I need the same attribute but in black.
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
More about styles on developer.android.com.

android dialog style properties

I want to change color of the blue parts in the following image:
I know how to use custom styles, I just need to know what are called the properties of these blue parts (I mean something like <item name="android:background">#FF0000</item> ) thx
What I really want to achieve is demonstrated here:
You may inherit the theme used from Android code and change particulars within that.
http://developer.android.com/guide/topics/ui/themes.html
<style name="GreenText" parent="#android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
parent holds the value of the default theme. In your case it will be the Dialog theme for Holo.

Categories

Resources