I want to simply apply styling to all buttons on a theme level like this
<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="buttonStyle">#style/DefaultButton</item>
</style>
<style name="DefaultButton" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">#color/whatever</item>
</style>
<Button
android:id="#+id/addChannelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="16dp"
android:text="Add room" />
Why doesnt this work? It would in appcompat
// If I use Theme.MaterialComponents.Light.NoActionBar.Bridge, then it works
You should be setting materialButtonStyle instead of buttonStyle in your theme.
Use a Theme.MaterialComponents theme defining the materialButtonStyle attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="materialButtonStyle">#style/MyButtonTheme</item>
</style>
In this way you can customize the theme of all the buttons in your app.
Also starting from version 1.1.0 of the material library you can override the theme attributes from the default style using the materialThemeOverlay attribute.
Something like:
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStyleTextColor</item>
</style>
<style name="ButtonStyleTextColor">
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
<!--the text color is colorOnPrimary -->
<item name="colorOnPrimary">#color/my_color</item>
</style>
Currently it requires version 1.1.0 of material components for android library.
Can you try with v1.0.0-alpha06? There has been some progress since v1.0.0 which may have addressed what you're seeing.
1.1.0-alpha02
Shape Theming: FloatingActionButton, MaterialButton, Chip, MaterialCardView, BottomSheet, & TextInputLayout updated to use the new Material shape system
1.1.0-alpha06
Implement Shapeable interface in MaterialButton
Source: https://github.com/material-components/material-components-android/releases
Use materialButtonStyle and use MaterialButton instead of Button
Related
I want to customize a Material chip.
I would think this is how to do it:
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
.... lots more theme stuff here
<item name="chipStyle">#style/MaterialChips</item>
<item name="chipGroupStyle">#style/MaterialChips</item>
<item name="chipStandaloneStyle">#style/MaterialChips</item>
</style>
<style name="MaterialChips" parent="Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">#color/chips</item>
</style>
None of the tags like chipStyle affect the chips. But if I set app:chipBackgroundColor="#color/chips" in xml it works.
It also works fine like this for other things like say <item name="materialAlertDialogTheme">#style/AlertDialogTheme</item>.
The material documentation (if you can call it that) is really not helping.
Your app theme is correct.
The default style used by Chip component is defined in the app theme by the chipStyle attribute.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Default style for chip component -->
<item name="chipStyle">#style/Widget.MaterialComponents.Chip.Action</item>
</style>
You can customize this style using for example:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<!-- Default value for chipStyle -->
<item name="chipStyle">#style/MaterialChips</item>
</style>
<style name="MaterialChips" parent="#style/Widget.MaterialComponents.Chip.Choice">
<!-- ... -->
<item name="chipBackgroundColor">#color/chips</item>
</style>
If you specify the style attribute in your layout, this style overrides the default value.
<com.google.android.material.chip.Chip
style="#style/Widget.MaterialComponents.Chip.Entry"
.../>
In this case the Chip uses the Widget.MaterialComponents.Chip.Entry style.
if you define chip style in layout xml, chip override your theme.
It may work if you clear chip style in layout xml.
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"/>
In an app of mine, I'm using the Theme.MaterialComponents.Light.NoActionBar as a base style. In this style, which I call AppTheme, I'm trying to override editTextStyle to provide a custom style for com.google.android.material.textfield.TextInputEditText (according to the source code, it uses R.attr.editTextStyle as a default style).
This is my current theme, related to the TIEditText and TILayout:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
[ primary and secondary colors, OnColors, etc.]
<item name="editTextStyle">#style/AppTheme.TextInputEditText</item>
<item name="textInputStyle">#style/AppTheme.TextInputLayout</item>
[ Custom attribute for testing, defined in attrs.xml ]
<item name="textInputEditTextStyle">#style/AppTheme.TextInputEditText</item>
</style>
For some reason, even though I set editTextStyle, if I use it in code, it does not get applied:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilFirstName"
style="?attr/textInputStyle"
android:hint="#string/label_firstname"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/firstName"
style="?attr/editTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="#={viewModel.firstName}" />
</com.google.android.material.textfield.TextInputLayout>
However if I replace the style of firstName with ?attr/textInputEditTextStyle, it works.
Why can't I override editTextStyle in the default theme? What the hell is going on?
Target SDK is 28, minSDK is 21, Material library version is 1.1.0-alpha06
Let's just move past the part where we all recognize that Android themes and styles are singularly the most absurd wasteland of hackery and guesswork ever devised by human beings.
This is an expansion on the previous answer. Same silly 'hack'. I was able to style the TextInputEditText by setting editTextStyle, but not where it intuitively belongs, but rather inside a custom materialThemeOverlay nested within the style defined for textInputStyle. Witness:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- works fine -->
<item name="textInputStyle">#style/AppTheme.TextInputLayoutStyle</item>
<!-- should work fine, doesn't work, happily ignored -->
<!-- <item name="editTextStyle">#style/AppTheme.TextInputEditTextStyle</item> -->
</style>
<style name="AppTheme.TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<!-- other props (boxBackgroundMode, boxBackgroundColor, boxStrokeColor, etc) -->
<!-- can we set editTextStyle from here? Of course not! We should magically know we need a material theme overlay-->
<item name="materialThemeOverlay">#style/AppTheme.MaterialThemeOverlay</item>
</style>
<!-- style inception! a style, child of another style, whose only purpose is to refer to yet another style -->
<style name="AppTheme.MaterialThemeOverlay">
<item name="editTextStyle">#style/AppTheme.TextInputEditTextStyle</item>
</style>
<!-- finally, the style we SHOULD have been able to set from the theme -->
<style name="AppTheme.TextInputEditTextStyle" parent="#style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:textColor">#color/white</item>
</style>
All of the above ridiculousness and ANOTHER day of my life thrown in the trash, just to change the color of text. Thaaaaanks Aaaaaandroid.
For some reason, even though I set editTextStyle, if I use it in code, it does not get applied
It happens because the default styles of the TextInputLayout override the editTextStyle using the materialThemeOverlay attribute.
For example the Widget.MaterialComponents.TextInputLayout.FilledBox has this default style:
<style name="Widget.MaterialComponents.TextInputLayout.FilledBox" parent="Base.Widget.MaterialComponents.TextInputLayout">
<item name="materialThemeOverlay">
#style/ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox
</item>
....
</style>
<style name="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox">
<item name="editTextStyle">#style/Widget.MaterialComponents.TextInputEditText.FilledBox</item>
</style>
Per https://material.io/develop/android/theming/typography/, I’ve been customizing my app’s style by modifying the textAppearanceX attributes. Does anyone know which attribute to customize to style an app’s Snackbars or where this is documented?
Thank you.
With the Material Components Library you can customize the textAppearance used by the text and the button through the app theme:
Just use the snackbarButtonStyle and snackbarTextViewStyle attributes to define globally in the app:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<!-- Style to use for action button within a Snackbar in this theme. -->
<item name="snackbarButtonStyle">#style/Custom.TextButton.Snackbar</item>
<!-- Style to use for message text within a Snackbar in this theme. -->
<item name="snackbarTextViewStyle">#style/Custom.Snackbar.TextView</item>
....
</style>
Then for the button you can define a custom style applying the android:textAppearance attribute
<style name="Custom.TextButton.Snackbar" parent="#style/Widget.MaterialComponents.Button.TextButton.Snackbar">
<item name="android:textAppearance">#style/snackbar_button_textappearance</item>
</style>
It is just an example:
<style name="snackbar_button_textappearance" parent="#style/TextAppearance.MaterialComponents.Button">
<item name="android:textStyle">italic</item>
</style>
For the text you can do something similar with:
<style name="Custom.Snackbar.TextView" parent="#style/Widget.MaterialComponents.Snackbar.TextView">
<item name="android:textAppearance">......</item>
</style>
Pls note:
snackbarButtonStyle attribute requires the version 1.1.0
snackbarTextViewStyle attribute requires the version 1.2.0.
I'm trying to apply default button style for theme. The theme is correctly applied to my app in the Manifest. In my gradle dependencies, I have design support library version 22.2.1 (this library has appcompat library as a dependency, so I guess I'm using the same version of the appcompat support library?)
When I define my theme like this, the button styles are applied correctly in the layout preview, but now when I launch the app:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">#style/Widget.Button</item>
</style>
When I define my theme like this, the button styles are applied correctly when app is launched, but not in the layout preview:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="buttonStyle">#style/Widget.Button</item>
</style>
Problem: I want this to work in both layout preview and in runtime, is there a way to achieve this?
Is this a bad solution?
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="buttonStyle">#style/Widget.Button</item>
<item name="android:buttonStyle">#style/Widget.Button</item>
</style>
Colored Button Style With AppCompat +22
in your styles.xml
<style name="Button.Tinted" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">YOUR_TINT_COLOR</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<item name="android:textColor">#android:color/white</item>
</style>
in your layout.xml
<Button
android:id="#+id/but_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/but_continue"
android:theme="#style/Button.Tinted" />
important use android:theme
https://gist.github.com/ingyesid/e513f47b573607205195