Android: Theme is not working - android

I am trying to apply a theme to android activity
themes.xml
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<!-- Application theme. -->
<style name="CableTheme" parent="AppBaseTheme">
<item name="textViewStyle">#style/Textview</item>
</style>
styles.xml
<style name="Textview" parent="#android:style/Widget.TextView">
<item name="android:textSize">22.0dp</item>
<item name="android:textColor">#android:color/holo_blue_bright</item>
</style>
Attrs.xml
<attr name="textViewStyle" format="reference" />
1) CableTheme is the applicaition theme . I expect all the textview should be in 22dp size and blue in color in the activity.But it is not rendering.
2) I want apply a style like text size 20 to all the textview in my activity . If above is not the correct procedure to apply theme. How can i do it?

It should be
<item name="android:textViewStyle">#style/Textview</item>
the attribute textViewStyle is from the framework. Therefore you have to put in the 'android' namespace before that.
Remove the textViewStyle attribute you defined yourself.

Related

ActionBar background with MaterialComponents theme

I want to customize my ActionBar. My Theme looks as below:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/ThemeOverlay.MaterialComponents.ActionBar">
<item name="background">#drawable/actionBarBackground</item>
</style>
In values folder:
<drawable name="actionBarBackground">#FFFFFFFF</drawable>
In values-night folder:
<drawable name="actionBarBackground">#FF000000</drawable>
Not sure why my ActionBar background colour doesn't change accordingly. I have tried to change my theme in different other ways as well but nothing works. Here are my other tries:
Instead of actionBarStyle, I used actionBarTheme.
<item name="actionBarTheme">#style/MyActionBar</item>
I also tried using colorPrimary.
<item name="colorPrimary">#color/actionBarBackground</item>
Am I missing something?
Since you are using a Theme.MaterialComponents.DayNight app theme the ActionBar background color is defined by default by the colorPrimary attribute.
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/mycolor</item>
</style>
where mycolor is define in res/values/colors.xml
<resources>
<color name="mycolor">#xxxxxx</color>
...
</resources>
You can also customize the background color using the actionBarStyle attribute in your app theme.
Something like:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">#style/Custom.ActionBar</item>
...
</style>
where:
<style name="Custom.ActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
<item name="background">#color/mycolor</item>
</style>
Otherwise you can use the actionBarTheme attribute to override the background color:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarTheme">#style/ThemeOverlay.ActionBar</item>
...
</style>
<style name="ThemeOverlay.ActionBar" parent="">
<item name="colorPrimary">#color/mycolor</item>
</style>
For me neither setting background nor colorPrimary has helped.
Apparently this is the intended behavior, as according to dark theme guidance for components that use a large portion of a screen. They should use colorSurface for their background instead of colorPrimary or colorAccent. AppBarLayouts, Toolbars, and ActionBars now default to their Surface styles in dark theme.
So I had to define colorSurface attribute for my theme in order for it to work.
<item name="colorSurface">#color/my_color_primary_dark</item>
I'm on the latest stable of MDC, and this is how it works here.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/purple_500</item>
<item name="actionBarStyle">#style/Widget.MaterialComponents.ActionBar.PrimarySurface</item>
</style>
If you want the app bar to be the same color as your color surface, change #style/Widget.MaterialComponents.ActionBar.PrimarySurface to #style/Widget.MaterialComponents.ActionBar.Surface
This is most likely because that's not the right way to declare colours in an app.
Typically, colour resources are located in one-singular file: colors.xml
This XML file is typically located in your app's resources folder (usually res/values/colors.xml)
They are then declared with a <resources> element as the root of the XML file and your colour resources (as defined with the <color> element):
<!-- Note: The XML header (aka <?xml version="1.0" ...?>) is optional -->
<resources>
<color name="your_color_res">#FFFFFF</color>
<!-- Your other colour resources go here -->
</resources>
You can then specify qualifiers for your colour resources by creating the same colors.xml in your app's resources with the following file format:
res/values-<qualifier>/colors.xml
These colours can then be referenced with the #color/ prefix in XML files or with R.color.* in your app's Java/Kotlin code.
Hope this helps!

Is there a way to define the src of an ImageView as an style id

I would like to make the image change based on the theme (for Day-Night mode).
I've tried creating an id with the name "logo" in the style.xml that contains the drawable reference for the logo and change it according to the style but I don't know how to set this id as the ImageView source.
In the style.xml:
<resources>
<item type="drawable" name="logo"></item>
...
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="logo">#drawable/logo</item>
...
<style name="AppThemeDark" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="logo">#drawable/logodark</item>
...
</resources>
I don't know how I would reference it in the ImageView as the intelisense can't seen to find any "logo" id when I try to write in "app:srcCompat" as you can see here.
How can I make the ImageView source change based on the theme?
Nevermind, just found out about tints.
The style.xml goes like this
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
...
<!-- This sets the image tint for the style. -->
<item name="android:tint">#color/colorPrimary</item>
...
</style>

Override Android background in custom style for one theme but not another

I'm using two themes for my Android application, AppTheme and AppThemeDark. I've set up a custom button style for each in the theme definitions:
<!-- Theme definitions -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:buttonStyle">#style/ButtonStyle</item>
...
</style>
<style name="AppThemeDark" parent="Theme.AppCompat.DayNight.NoActionBar">
...
<item name="android:buttonStyle">#style/ButtonStyleDark</item>
...
</style>
<!-- Button styles -->
<style name="ButtonStyle" parent="#style/Widget.AppCompat.Button">
<item name="android:ellipsize">end</item>
<item name="android:maxLines">2</item>
</style>
<style name="ButtonStyleDark" parent="ButtonStyle">
<item name="android:background">#drawable/gray_button_bg</item>
</style>
Note that my AppTheme button, ButtonStyle, doesn't override the background of Widget.AppCompat.Button, but AppThemeDark's button style inherits ButtonStyle and it changes the background.
Now, I would like to extend that buttonStyle with a new custom style for special buttons, e.g. PrimaryButton. In the AppTheme case, I do not want to change the background of the button. In the AppThemeDark case, i do want to change the background of the button.
I would like to define PrimaryButton and have it either inherit the background (in the case of AppTheme) or use a new background (AppThemeDark). Something like this:
<style name="PrimaryButton">
<item name="android:background">?attr/drawablePrimaryButtonBackground</item>
</style>
But as far as I can tell, there's no way to define an attribute in AppThemeDark as a new drawable and AppTheme as "inherit from parent". Setting to transparent obviously makes the button background in AppTheme transparent.
Is my only option to figure out what drawable is being used for a background in Widget.AppCompat.Button and define it locally?
To answer my own question...
You can dig up the private Android resource and use a local copy to retain the background. That locks you into one specific Android version of the drawable though (unless you copy multiple versions over).
You can set whole styles as attributes if the styles between your two themes are different enough to warrant it:
attrs.xml:
<attr name="stylePrimaryButton" format="reference" />
styles.xml:
<!-- Theme definitions -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="stylePrimaryButton">#style/PrimaryButton</item>
...
</style>
<style name="AppThemeDark" parent="Theme.AppCompat.DayNight.NoActionBar">
...
<item name="stylePrimaryButton">#style/PrimaryButtonDark</item>
...
</style>
<!-- Primary button styles -->
<style name="PrimaryButton">
<!-- no need to override anything -->
</style>
<style name="PrimaryButtonDark">
<item name="android:background">#drawable/dark_background</item>
</style>
layout.xml:
<Button style="?attr/stylePrimaryButton"
...

Custom button style shown in GUI preview, but not in actual app

styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:buttonStyle">#style/button_style</item>
</style>
<style name="button_style" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/simple_button</item>
</style>
</resources>
And I've verified that simple_button works - if I manually assign it to a button's background, it shows up in the app.
Do you have only one styles.xml file? Check this.
Generally has a number of styles.xml files. (ex> values/styles.xml, values-v21/styles.xml, etc...) and Check your device information.
Try using
<item name="buttonStyle">#style/button_style</item>
instead of:
<item name="android:buttonStyle">#style/button_style</item>

How to define attr value for Theme dynamically

Let me clarify the question.
I have theme like this
<style name="DefaultTheme" parent="android:Theme.Holo.Light">
<item name="android:textViewStyle">#style/DefaultTextViewStyle</item>
</style>
where
<style name="DefaultTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">?attrTextColor</item>
</style>
for application I use the DefaultTheme theme, and I want to change value of the attrTextColor dynamically from code (to change text color for all TextView). Can I do it? How Can I do it?
For simple explication, you can do it with theme.
You have to define your attributes
<attr name="color_menu" format="color"/>
<attr name="color_text_main" format="color"/>
Define your theme color (here, blue and green)
<style name="AppTheme_green" parent="DefaultTheme">
<item name="color_menu">#color/very_dark_green</item>
<item name="color_text_main">#color/very_dark_gray3</item>
</style>
<style name="AppTheme_blue" parent="DefaultTheme">
<item name="color_menu">#color/very_dark_blue</item>
<item name="color_text_main">#color/very_dark_gray1</item>
</style>
Now your current activity can have juste one theme in your AndroidManifest.xml
<activity
android:theme="#style/AppTheme_green"
>
...
</activity>
But you can change it dynamically in your activity
activity.setTheme(R.style.AppTheme_Blue);
I hope this can help you

Categories

Resources