I´m Using com.android.support:appcompat-v7:22.1.1
This attribute android:buttonStyle is not setting that style for every button, I need to set android:theme="#style/AppTheme.Button" manually on each button.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">#style/AppTheme.Button</item>
</style>
What do I need to do to set a general style for it?
Use buttonStyle insteadof android:buttonStyle in your theme
Similar to #danielgomezrico answer but without having two style files:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- android:buttonStyle for v21+ and buttonStyle for the rest -->
<item name="buttonStyle">#style/MyCustomButton</item>
<item name="android:buttonStyle">#style/MyCustomButton</item>
<item name="colorButtonNormal">#color/my_color_accent</item>
</style>
<style name="MyCustomButton" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#ffffff</item>
</style>
I ended up setting background color with colorButtonNormal attribute and other style attributes with buttonStyle.
values/style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/my_color_primary</item>
<item name="colorPrimaryDark">#color/my_color_primary_dark</item>
<item name="colorAccent">#color/my_color_accent</item>
</style>
<style name="AppTheme.Base">
<item name="colorButtonNormal">#color/my_color_accent</item>
<item name="buttonStyle">#style/Button</item>
</style>
<style name="Button" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#color/my_color_white</item>
</style>
values-v21/style.xml
<style name="AppTheme.Base">
<item name="android:colorButtonNormal">#color/my_color_accent</item>
<item name="android:buttonStyle">#style/Button</item>
</style>
And then all the buttons have the background/text color I wanted without setting style on each one.
Related
In androidx you can easily toggle between day/night mode. E.g.:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- attributes -->
</style>
And when toggling theme:
AppCompatDelegate.setDefaultNightMode(nightMode);
getDelegate().applyDayNight();
Now, let's say I want to add a minor customization to either the day or the night theme:
<style name="LimeTheme" parent="AppTheme">
<item name="colorPrimary">#color/lime1</item>
<item name="colorPrimaryDark">#color/lime2</item>
<item name="colorControlHighlight">#color/lime3</item>
</style>
How do I accomplish that?
May be you need a folder —— [values-night].
In your theme.xml(or style.xml), you can set the day theme like :
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" >
<item name="minor customization">#style/ThemeOverlay.MyTheme.DayCustom</item>
</style>
In your theme-night.xml(or style-night.xml):
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" >
<item name="minor customization">#style/ThemeOverlay.MyTheme.NightCustom</item>
</style>
And in style,you shoule init these:
<style name="ThemeOverlay.MyTheme.NightCustom" parent="">
<item name="colorPrimary">#color/nightLime1</item>
<item name="colorPrimaryDark">#color/nightLime2</item>
<item name="colorControlHighlight">#color/nightLime3</item>
</style>
<style name="ThemeOverlay.MyTheme.DayCustom" parent="">
<item name="colorPrimary">#color/dayLime1</item>
<item name="colorPrimaryDark">#color/dayLime2</item>
<item name="colorControlHighlight">#color/dayLime3</item>
</style>
The key is in ThemeOverlay, the parent of ThemeOverlay.MyTheme.DayCustom or ThemeOverlay.MyTheme.NightCustom,is "",because the system will automatic recognition it is ThemeOverlay, and just change the style you set,like colorPrimary,colorPrimaryDark...
I have a project and in this project I have defined fontFamily to all textviews in the style of AppTheme.
The fontFamily is montserrat but in text views I use montserrat_semibold, montserrat_regular and montserrat_medium.
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textViewStyle">#style/MontserratTextViewStyle</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MontserratTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat</item>
</style>
How can I change their weight in the different XMLs of textviews?
Hmm the only way I can think of doing that is to create multiple styles in your styles.xml:
<style name="MontserratTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat</item>
</style>
<style name="MontserratTextViewSemiBoldStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_semibold</item>
</style>
<style name="MontserratTextViewRegularStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_regualr</item>
</style>
<style name="MontserratTextViewMediumStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">#font/montserrat_medium</item>
</style>
Then for any TextView which doesn't use the default montserrat i.e. maybe you want montserrat_medium:
<TextView
style="#style/MontserratTextViewMediumStyle"
....
/>
I am using a custom theme for dialogs in my app.
The custom theme applies to the alert dialogs just fine.
But the theme does not apply on the preference dialogs like ListPreference, CheckboxPreference etc.
Currently i am applying the dialog theme using the materialAlertDialogTheme attribute like this:
Main app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:navigationBarColor">?attr/colorPrimary</item>
<item name="android:windowTranslucentStatus">true</item>
<!-- Material Dialog theme -->
<item name="materialAlertDialogTheme">#style/ThemeOverlay.App.MaterialAlertDialog</item>
</style>
MaterialDialog styles:
<!-- Styles for Material Dialogs -->
<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorSurface">#android:color/white</item>
<item name="alertDialogStyle">#style/MaterialAlertDialog.App</item>
<item name="materialAlertDialogTitleTextStyle">#style/MaterialAlertDialog.App.Title.Text</item>
<item name="buttonBarPositiveButtonStyle">#style/Widget.App.PositiveButton</item>
</style>
<style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.MaterialComponents">
<item name="shapeAppearance">#style/ShapeAppearance.App.MediumComponent</item>
</style>
<style name="MaterialAlertDialog.App.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textStyle">bold</item>
</style>
<style name="Widget.App.PositiveButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="materialThemeOverlay">#style/ThemeOverlay.App.Button</item>
<item name="shapeAppearance">#style/ShapeAppearance.App.SmallComponent</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="ThemeOverlay.App.Button" parent="">
<item name="colorPrimary">#android:color/black</item>
</style>
<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">4dp</item>
</style>
Here is a dialog perfectly getting themed:
And here is a preference dialog which is not getting themed (notice the corners):
I have also tried setting alertDialog attribute in the main theme, but its not working.
Any suggestions?
What I needed to resolve this issue was to add
<item name="alertDialogTheme">#style/AppTheme.AlertDialogTheme</item>
to my base style and
<style name="AppTheme.AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="dialogCornerRadius">20dp</item>
</style>
as a style. Sad that this made my whole AlertDialog.Builder to MaterialAlertDialogBuilder migration somehow pointless (at least in the cases cornerFamily is rounded), hopefully we will see a better integration between preferences and material theme in future.
reference: https://www.reddit.com/r/androiddev/comments/dg8jfo/styling_androidx_preference_dialogs_using/
When i'm using the item actionBarStyle, colorPrimary doesn't set the color to red.
If i delete the item actionBarStyle, it works. How can i change my code to display the color?
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyStyledActionBar</item>
<item name="colorPrimary">#color/red</item>
</style>
<style name="MyStyledActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
</resources>
The root of Widget.AppCompat.Light.ActionBar is Base.Widget.AppCompat.ActionBar, which doesn't apply a background attribute.
<style name="Base.Widget.AppCompat.ActionBar" parent="">
...
<item name="background">#null</item>
<item name="backgroundStacked">#null</item>
<item name="backgroundSplit">#null</item>
...
</style>
Instead your parent should be Widget.AppCompat.Light.ActionBar.Solid, so that you'll inherit the attributes from Base.Widget.AppCompat.Light.ActionBar.Solid, which applies the background attribute.
<style name="Base.Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">?attr/colorPrimary</item>
<item name="backgroundStacked">?attr/colorPrimary</item>
<item name="backgroundSplit">?attr/colorPrimary</item>
</style>
Alternatively, you could just apply the background attribute yourself.
<style name="MyStyledActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
<item name="background">?attr/colorPrimary</item>
</style>
I want to add a custom style that is for example purple in one theme and red in one other theme.
<style name="subtitle_layout" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#color/purple</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40sp</item>
</style>
<style name="subtitle_layout2" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#color/black</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">80sp</item>
</style>
<style name="Theme1" parent="android:Theme.Holo.Light">
<item name="android:textColor">#color/black</item>
<item name="#style/subtitle_layout">#style/subtitle_layout</item>
</style>
<style name="Theme2" parent="android:Theme.Holo.Light">
<item name="android:textColor">#color/white</item>
<item name="#style/subtitle_layout">#style/subtitle_layout2</item>
</style>
The change of theme works because i see the change of text color, but instead of android:textColor i would like to put a style instead. But it doesn't work.
I guess you are trying to inherit existing styles to your theme, am I right? You can just set the style as your parent theme:
<style name="Theme1" parent="#style/subtitle_layout">
<item name="android:textColor">#color/black</item>
</style>