I need to apply a background color to all the buttons in my application, so I added this to styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- button styles -->
<item name="colorButtonNormal">#color/button_color</item> <!-- below api 21 -->
<item name="android:colorButtonNormal">#color/button_color</item> <!-- above api 21 -->
<item name="android:buttonStyle">#style/ButtonColor</item>
</style>
<style name="ButtonColor" parent="#style/Widget.AppCompat.Button">
<item name="android:textColor">#color/font</item>
</style>
It worked perfectly, when I add a button in XML, the button has automatically the colors I set in styles.xml
The problem is that when I add a ImageButton, the ImageButton is getting also those colors, and I don't want that. The problem is specifically with colorButtonNormal, which applyes the background color to the ImageButton also and I don't want that. How can I solve this problem?
The default style used by the ImageButton (in your case AppCompatImageButton) is defined in the app theme by the attr:
<item name="imageButtonStyle">#style/Widget.AppCompat.ImageButton</item>
In this style the background attribute <item name="background">#drawable/btn_default_material</item> is tinted with the android:tint="?attr/colorButtonNormal".
You can use a custom style starting from btn_default_material for the ImageButton or you can override the color in the layout.
Something like:
<ImageButton
android:theme="#style/MyImageButtonTheme"/>
with something like:
<style name="MyImageButtonTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorButtonNormal">#color/my_color</item>
</style>
Also evaluate to migrate to Material Components and the MaterialButton.
In this case it is very simple to override the color just using the styles:
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStylePrimaryColor</item>
</style>
<style name="ButtonStylePrimaryColor">
<item name="colorPrimary">#color/....</item>
</style>
You can do it by applying different style for defferent views
Let's say i have three style with defferent color combination and i wish to apply bigButtonTheme style for all big button
Then
i add this line on all big button android:theme="#style/bigButtonTheme">
Rest button or other vewis will remain unchanged.
You can also copy other properties of a style like global themes font background color by deffineing as parents theme
<style name="ButtonColor" parent="#style/GlobelStyleOfApp"/>
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 am trying to understand how to change the look of all checkboxes within my application for a certain theme.
I would like to change the checkbox check color and the checkbox border color.
If I create this style:
<style name="MyCheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
And apply it as a theme on my checkbox it works great.
<CheckBox
android:id="#+id/status_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyCheckBox"/>
However I don't really want to apply that theme to every checkbox, I would like to set the theme for all checkboxes.
I tried to set the checkboxStyle in my theme however this doesn't work.
<style name="MyTheme" parent="Theme.AppCompat">
...
<item name="checkboxStyle">#style/MyCheckBoxStyle</item>
...
</style>
<style name="MyCheckBoxStyle" parent="Base.Widget.AppCompat.CompoundButton.CheckBox">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
Is there a way to set a default theme for all checkboxes?
Unfortunately, colorControlNormal and colorControlActivated are theme attributes, not style attributes, so they only work if they're defined in the view's theme. There is no way that I know of to set a "default theme" for all views of a certain type; attributes like checkboxStyle can only set a default style for all checkboxes. Additionally, you can't "trick" the system by writing something like:
<style name="MyTheme" parent="Theme.AppCompat">
...
<item name="checkboxStyle">#style/MyCheckBoxStyle</item>
...
</style>
<style name="MyCheckBoxStyle" parent="Base.Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:theme">#style/MyCheckboxTheme</item>
</style>
<style name="MyCheckboxTheme">
<item name="colorControlNormal">#8AFFFFFF</item>
<item name="colorControlActivated">#android:color/white</item>
</style>
Your choices are:
Modify colorControlNormal and colorControlActivated in your app's theme
Modify colorControlNormal and colorControlActivated in your activity's theme
Manually set the android:theme attribute to every checkbox you want to change (or to the parent viewgroup holding these checkboxes)
Edit: potential workaround
Depending on your exact requirements, you might be able to get away with this:
<style name="MyTheme" parent="Theme.AppCompat">
<item name="checkboxStyle">#style/MyCheckboxStyle</item>
</style>
<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="buttonTint">#8AFFFFFF</item>
</style>
This will change the color of every checkbox in your app without requiring the use of theme attributes. You can even use a color selector resource for buttonTint if you want different colors for checked/unchecked:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"/>
<item android:color="#8AFFFFFF"/>
</selector>
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"
...
I would like to set a specific style for the whole application.
I added the style in the androidmanifest.xml, and I defined the style as follow:
<style name="myStyle">
<item name="android:background">#000000</item>
<item name="android:textColor">#FFFFFF</item>
</style>
This style changes the background and the textcolor for the whole application, though I would like to change also the text color of the buttons: this sentence works when defining a button but I would like to add it to the style of the application
Thanks a lot
<style name="myStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/Your button style</item>
</style>
source:How do I apply a style to all buttons of an Android application
I have two holo based ABS styles, Purple and Lime, users are able to set the theme from settings.
In my layout I have a TextView with a custom textAppearance, I want to change that textAppearance based on the active style.
(if the purple theme is activated the text must be white and if the lime theme is activated the text must be lime)
Is there a way to do that from the XML?
Edit
I'm sorry if the title is misleading.
(if the purple theme is activated the text must be white and if the
lime theme is activated the text must be lime)
Try that:
<style name="PurpleTheme" parent="...">
<!-- define the style for the text appearance here. Following is an example -->
<item name="android:textAppearance">#style/MyWhiteText</item>
</style>
<style name="LimeTheme" parent="...">
<!-- define the style for the text appearance here. Following is an example -->
<item name="android:textAppearance">#style/MyLimeText</item>
</style>
<style name="MyWhiteText" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/white</item>
</style>
<style name="MyWhiteText" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/lime</item>
</style>
Or if you don't want to set the color to all TextViews then do this:
Declare an attribute:
<attr name="myTextViewColor" format="reference" />
Use it like this in your theme:
<style name="PurpleTheme" parent="...">
<item name="myTextViewColor">#color/white</item>
</style>
<style name="LimeTheme" parent="...">
<item name="myTextViewColor">#color/lime</item>
</style>
Now you can set the color of the specific TextView like this:
<TextView
android:textColor="?attr/myTextViewColor"
[...] />