I have a project where I defined all the styles in the theme (Button style, Checkbox Style, EditText style and so on) This way I don't need to apply any style or theme in the layouts where I use those views, because they are applied automatically by my AppTheme.
Now I encountered a problem. I wanted to define the Switch style inside the theme but it should use another color for the colorControlActivated and colorControlHighlight. By default it uses the colorPrimary which I defined in the theme, but what if I want to change that.
The problem can be fixed easy with a theme overlay or a style where I override the needed attributes that I mentioned above and apply that style/theme everywhere where I use the Swtch view. But I want to know if I can avoid that and define a default style for my Switch views inside the same theme where the colorControlActivated and colorControlHighlight are already defined.
I tried several things but this looked like the one that actually might work but it does not:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
<item name="colorSwitchThumbNormal">#color/white</item>
<item name="switchStyle">#style/SwitchStyle</item>
</style>
<style name="AppTheme.GreenControlOverlay">
<item name="colorControlActivated">#color/green</item>
<item name="colorControlHighlight">#color/green</item>
</style>
the SwitchStyle looks like this
<style name="SwitchStyle" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="android:theme">#style/AppTheme.GreenControlOverlay</item>
</style>
I dont know why this is not working because if I set the android:theme inside my AppTheme directly it does override the colorControl attributes, but if you override it from a style it does not work. If I apply this GreenControlOverlay on the Switch view inside of my layout it also works.
Is it even possible to do this?
If you have any questions please don't hesitate to ask. I hope I explained my problem well.
Related
The "colorprimary" changes in themes.xml then the button colour also changes "(image in lite mode)enter image description here (image in dark mode)enter image description hereedittext hint selection colour also changes . I want to edit text colour with out changing the theme and without changing button colour
I think, is important to mention first that a theme is different than a style. Basically, when you apply a style it only affects to the component( edit text, view, textview, etc). But when you apply a theme it affects in general terms to the app. More info about in this Link.
So, if you are applying a theme probably you are modifying some attributes that are being used in the app scope such as primaryColor, secondaryColor, etc. Probably, the editText is using primaryColor in order to set the color of the hint and when you change to another theme, primaryColor is affected, then the color hint of the editText is affected as well.
Based on the above description, we can apply multiple solutions. I would like to suggest the following.
default theme.xml
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/gray_medium_dark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimaryVariant">#color/gray_dark</item>
<item name="colorSecondary">#color/cyan</item>
<item name="editTextStyle">#style/CustomEditTextStyle</item>
</style>
nigh theme theme.xml
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowLightNavigationBar" tools:ignore="NewApi">false</item>
</style>
style.xml
<style name="CustomEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:textSize">#dimen/text_size_16sp</item>
<item name="android:textColorHint">#color/gray_normal_light</item>
<item name="android:fontFamily">#font/roboto_condensed</item>
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:inputType">textVisiblePassword</item>
</style>
Explanation: Another important point about style and themes is that we can apply something similar to inheritance. It means that we ca re-use some attributes or/and modify others.
So, <item name="editTextStyle">#style/CustomEditTextStyle</item> means that for all the edittext on my application I want to set up CustomEditTextStyle style. Also, in nigh theme we are applying parent as parent="AppTheme.Base" and this parent also has CustomEditTextStyle as style for edittext, so it will no change.
I want to set item elevation in some of my app's styles. Now elevation is only 21 and higher with no support library, so my natural inclination was to just create a styles-v21 xml and place it in there:
<style name="Widget.MyApp.Drawer" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:elevation">4dp</item>
</style>
The problem with this is any changes I make to Widget.MyApp.Drawer in the regular styles.xml file will be overwritten by this completely. What I'd want is for elevation to just be tacked on to the bottom of the list of style changes I made for the v21 version of this style listed in styles.xml.
So I took to creating base styles which the style I use in the views inherits from:
<style name="BaseListElement">
<item name="android:background">#drawable/listitem_background</item>
<item name="android:layout_height">#dimen/list_item_height</item>
</style>
<style name="BaseListElement.ListItem">
</style>
I leave the style blank in styles.xml, and in styles-v21, I add elevation and it works.
However this get's kind of tricky when I want to use some advanced styles:
<style name="BaseListElement">
<item name="android:background">#drawable/listitem_background</item>
<item name="android:layout_height">#dimen/list_item_height</item>
</style>
<style name="BaseListElement.BaseItem">
<item name="android:padding">#dimen/list_item_padding</item>
</style>
<style name="Widget.MyApp.ListItem" parent="#style/BaseListElement.BaseItem">
</style>
<style name="BaseListElement.BaseHeader">
</style>
In this case, BaseItem is just one style that inherits from BaseListElement, styles such as BaseHeader inherit from it as well. This is getting kind of ridiculous as you can see.
Am I overthinking this? The way I see it I have 3 choices here:
1) Continue as is and feel like an idiot
2) On the BaseListElement level, create a child style with some goofy name which is the point at which I apply the elevation, which would then (hopefully) trickle down to all the children. As soon as I have a difference between v21 children of the base however, this wouldn't work.
3) Just throw android:elevation into the styles.xml file (don't use a v21 file) and place an ignore flag on the element. I only have 5.0 devices here, so I can't easily test at the moment if this will cause a crash on older versions.
Any thoughts?
To accomplish something like this you could just create a BaseListElement.BaseItem in both styles.xml and syles-v21.xml the first one without the elevation and the second one with it. Then just extend Widget.MyApp.ListItem from BaseListElement.BaseItem which should get updated in v21 to use the elevation.
styles.xml
<style name="BaseListElement.BaseItem">
</style>
<style name="Widget.MyApp.ListItem" parent="#style/BaseListElement.BaseItem">
</style>
styles-v21.xml
<style name="BaseListElement.BaseItem">
<item name="android:padding">#dimen/list_item_padding</item>
</style>
Method 3 you can safely implement as follows:
<item name="android:elevation" tools:ignore="NewApi">4dp</item>
I'm trying to put a style in all my app, so i created a theme with my style inside :
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
</resources>
But textAppearance doesn't work it stay the same, but when i put something like textColor in my theme, it works
This is a quite old question, but the answer may help someone.
The key to solve this is in the "precedence order of styling techniques" here:
on the top is the highest precedence, at the bottom is the lowest precedence.
As we can see theme has the lowest precedence, in your example, your android:textAppearance property is being overridden by the default style of every view that accepts this attribute, the default style property is defined in every them for every specific view that accepts this attribute, in this case android:Theme.Holo.Light provides the default style for textView as android:textViewStyle... for buttons is android:buttonStyle (which inherits its textAppearance from TextView), and so on.
So if you are trying to apply that android:textAppearance property to a TextVew you should use <item name="android:textViewStyle">#style/subtitle</item> instead of <item name="android:textAppearance">#style/subtitle</item> inside MyTheme. Away to veryfy this is setting android:textViewStyle to null, that way your current code will work fine with textViews <item name="android:textViewStyle">null</item>
This post explains this precedence a bit deeper:
https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d
What I can see is, you have not declared the color in your xml for theme. Please add the following line within the <resources> and try. Your xml will look like:
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<color name="purple">code for your color</color>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
I think this will do.
Depends on your target API you need to put your customization code in different /res/values-vxx/style.xml files.
For TextView, try android:textAppearanceSmall inside your theme instead.
I would like to know if it is possible (and how) to customize an existing theme.
What I'm looking for is how can I retrieve a certain attribute (i.e. color) and change it when the Activity starts and reapply the modified theme before setContentView().
Similar to setTheme(), but instead of using a resource id, use the modified theme.
Why not just make your own theme, setting the android:parent to the theme you want to copy, then set your own attributes? That is demonstrated in this documentation, like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
In this case, the CodeFont style will be identical to the TextAppearance.Medium style, except for the items specified here. You can do the same with any theme, including the default Holo or Dark theme or whatnot.
Based on further research and Eric's comment there is not yet a possible way to modify a theme programmatically. Different themes can be applied programmatically but not modified. Once the style is set in XML, it cannot be modified.
I am trying to change the blue background of a focussed/pressed menuitem in the overflow menu.
I'm talking about this:
I think I've overwritten every possible attribute from the Themeand Theme.Holo in Themes.xml and went to each style in Styles.xml but couldn't change the blue background.
EDIT
i figured out that the style i should override is android:dropDownListViewStyle
but it didn't work at all.
but then i changed the theme from Theme.Holo.Light.DarkActionBar to Theme.Holo.Light and guess what? it worked!!
so, can anyone shed some light on this?? how can i change the color on a theme with DarkActionBar??
ok, i found a solution for my problem: link to a post in the ActionBarSherlock google group
i've overseen this entry: android:actionBarWidgetTheme in the Theme.Holo.Light.DarkActionBar theme.
now all i had to do is define a theme that overrides Theme.Holo with one style in it: <item name="android:dropDownListViewStyle">#style/myDropDownListView</item> and point to this theme instead.
<!-- theme referenced by actionBarWidgetTehme style -->
<style name="Theme.DropDown.Dark" parent="android:style/Theme.Holo">
<item name="android:dropDownListViewStyle">#style/myDropDownListView</item>
</style>
<!-- my main theme -->
<style name="DarkActionBarRedActionMode" parent="android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarWidgetTheme">#style/Theme.DropDown.Dark</item>
</style>
declare a selector drawable, and put it as background of listview item.
refer to drawable resource