Android FAB icon always black with MaterialComponents theme - android

I'm creating an Android app and I'm using the AndroidX libraries and Material design theme. My app theme on styles.xml is:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
I have the following FAB from a custom library:
<com.leinardi.android.speeddial.SpeedDialView
android:id="#+id/work_log_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:layout_behavior="#string/speeddial_scrolling_view_snackbar_behavior"
app:sdMainFabClosedSrc="#drawable/ic_add_white_24dp"
app:sdOverlayLayout="#id/overlay" />
And also tried the default FAB:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_add_white_24dp"
android:layout_margin="16dp" />
No mater the color of the icon (a vector drawable), the icon inside the FAB (from the library and from the default) is always black. I have narrowed down the problem to the material design theme, since using the old Theme.AppCompat.Light.DarkActionBar instead of the new Theme.MaterialComponents.Light.DarkActionBar the icon inside the FAB gets the color of the original vector drawable.
Does anyone know why this is happening and how to solve it?

I solved this by using:
app:tint="#color/COLOR_OF_ICON"
and NOT:
android:tint="#color/COLOR_OF_ICON"
Reference: https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingActionButton.md

For MaterialComponents Theme you can define the following colors.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorPrimaryVariant">#color/primaryVariant</item>
<item name="colorOnPrimary">#color/onPrimary</item>
<item name="colorSecondary">#color/secondary</item>
<item name="colorSecondaryVariant">#color/secondaryVariant</item>
<item name="colorOnSecondary">#color/onSecondary</item>
<item name="colorAccent">#color/accent</item>
<item name="colorSurface">#color/surface</item>
<item name="colorOnSurface">#color/onSurface</item>
<item name="colorError">#color/error</item>
<item name="colorOnError">#color/onError</item>
<item name="colorButtonNormal">#color/buttonNormal</item>
<item name="colorControlNormal">#color/controlNormal</item>
<item name="colorControlActivated">#color/controlActivated</item>
<item name="colorControlHighlight">#color/controlHighlight</item>
<item name="colorBackgroundFloating">#color/backgroundFloating</item>
</style>
colorSecondary is the responsible color for FloatingActionButton.
and
colorOnSecondary is the responsible color for icon color of FloatingActionButton.
In your AppTheme you have not defied the colorSecondary. So, it took the default black color from parent Theme.MaterialComponents.Light.DarkActionBar.
Reference : Android dev summit, 2018 - The Components of Material Design

As said in this answer, if your icon has multiple colors or if you want to keep your icon original color(s), then assign #null as the tint:
app:tint="#null"

According to the GitHub documentation page for the Material Components library's FloatingActionButton, the only attributes that affect the icon are
app:srcCompat
app:tint
app:maxImageSize
In this case, since your color is defined as a constant (#FFF), the only one that seems to make sense is app:tint. Perhaps something in your theme has set this to black?
You ought to be able to override it by setting app:tint="#FFF" on your FAB.

Is Your Icon is multicolor
Add Only
app:tint="#null"
but if you only want to change color
#dimen/fab_margin -> 16dp
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
style="#style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/colorAccent"
app:srcCompat="#drawable/ic_add"
app:tint="#color/colorWhite" />
Style :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Programmatically in kotlin :
fab.setImageDrawable(ContextCompat.getDrawable(context!!, R.drawable.your_drawable))

In my case, I just follow this
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/bottom_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_home"
android:scaleType="centerCrop"
android:src="#drawable/ic_add_sign"
app:backgroundTint="#color/app_dark_green"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchor="#id/nav_view"
app:tint="#FFF" />
And my result is

use app:tint="#color/white"
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:tint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="#drawable/ic_done"/>

I was looking for it and the following code worked.
android:backgroundTint="#color/primarycolor"
android:textColor="#color/white"
app:iconTint="#color/white"

Using vector asset as icon for FAB . Here is what worked for me .
app:tint is changing vector icon color and app:backgroundTint is changing background of FAB .
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/idFABAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="bottom"
android:layout_gravity="bottom|right"
android:layout_marginBottom="70dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="18dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="18dp"
app:srcCompat="#drawable/ic_baseline_home_24"
app:tint="#color/white"
app:backgroundTint="#color/purple_500"
android:contentDescription="TODO" />

I'm also using Heinardi speed dial library. According to the document about customizing items, you can define a theme for the component:
<style name="Theme.FAB">
<item name="colorPrimary">#color/Primary</item>
<item name="colorAccent">#color/Accent</item>
<item name="colorOnSecondary">#color/white</item>
</style>
Note that colorPrimary defines the color of extended buttons,
colorAccent the color of main button,
colorOnSecondary the color of icon.
Then you can add the theme to SpeedDialView:
<com.leinardi.android.speeddial.SpeedDialView
android:id="#+id/speedDial"
android:theme="#style/Theme.FAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:sdMainFabClosedSrc="#drawable/ic_add_white"
app:sdOverlayLayout="#id/speedDialOverlay" />

app:backgroundTint="#color/white"
app:fabSize="mini"
app:srcCompat="#drawable/ic_"
app:tint="#null"

Color of icons are overrided by colorOnSecondary (see themes.xml).
To put the icon colour you want you must use:
app:tint="#colour/icon_colour"
And for the background:
app:backgroundTint="#colour/icon_colour"
You can change these colours automatically by the theme using https://stackoverflow.com/a/24043959/1200914

Just want to add my 2 cents. There are a few options how to fix your color issue for Material Components. The majority of them are already covered in the answers.
However, there's another and, probably, more general approach in this case. You can set a default "floatingActionButtonStyle" to your theme which will be used for all FloatingActionButtons.
<style name="Theme.Design" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="floatingActionButtonStyle">#style/Theme.Design.FloatingActionButton</item>
</style>
<style name="Theme.Design.FloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton">
<item name="tint">#null</item>
</style>
More details about styling of FloatingActionButtons is available there Theming Fabs

UPDATED
Shortly:
#gab-ledesma comment did the trick:
https://stackoverflow.com/a/53843325/5451349
implementation 'com.google.android.material:material:1.2.0-alpha05'
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
...
app:iconTint="#color/white"
app:icon="#drawable/ic_add_white"
...
Finnaly app:iconTint changed an icon color.
I've found it in the source code:
https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml#L81
Of cource, you need to check the instruction before:
https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

Related

drawable start not working with material button

Here is the button :
<Button
android:id="#+id/btn_choose_photo"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_camera"
android:drawablePadding="8dp"
android:text="#string/image_picker_dialog_choose_image"
android:textAlignment="textStart" />
I am using material themes, so this will be inflated into a material button
drawableStart has no effect at all, however drawableEnd, bottom and top work just fine
When I make the button tag a text view, drawableStart works
It seems like a bug or maybe I am missing something ?
Edit: My app theme is as follows :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!---colors-->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryVariant</item>
<item name="colorPrimaryDark">#color/colorPrimaryVariant</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryVariant</item>
<item name="colorAccent">#color/colorSecondary</item>
<!--
<item name="android:colorBackground">#color/colorBackground</item>
-->
<!--components-->
<item name="textInputStyle">#style/text_input_layout_style</item>
<item name="bottomSheetDialogTheme">#style/bottom_sheet_dialog_theme</item>
<item name="spinnerStyle">#style/spinner_style</item>
<item name="android:spinnerStyle">#style/spinner_style</item>
<item name="android:toolbarStyle">#style/toolbar_style</item>
<item name="toolbarStyle">#style/toolbar_style</item>
<item name="actionOverflowButtonStyle">#style/overflow_button_style</item>
<item name="android:actionOverflowButtonStyle">#style/overflow_button_style</item>
</style>
You should use app:icon like this:
In the layout:
<Button
...
app:icon="#drawable/ic_camera"
style="#style/Widget.MaterialComponents.Button.TextButton"
/>
It is displayed at the start, before the text label. You can change icon gravity, tint or size.
For more information
Using
android:drawableLeft
will solve the problem, but will not give you RTL(Right to left) support. If your end user uses a different language which follow RTL, then your Button will not support it.
Are you managing the Button's property in the Activity or Fragment, as technically speaking,
android:drawableStart
should work.
You can change to "App compat button"
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/continuar_anonimo_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
app:backgroundTint="#color/greenPrimary"
android:background="#drawable/corner_boton_outline"
android:text="Continuar anonimamente"
android:paddingLeft="15dp"
android:drawableStart="#drawable/ic_google_color"
style="?android:textAppearanceSmall"/>
Use android:drawableLeft
android:drawableLeft="#drawable/ic_phone"
android:backgroundTint="#android:color/white"

Default color change in ContentLoadingProgressBar

I am using androidx in my project and somewhere i am using ContentLoadingProgressBar but i am not able to change default color.
I have tried with android:indeterminateTint but can't help.
If anyone having any idea regarding this plz suggest
<androidx.core.widget.ContentLoadingProgressBar
android:id="#+id/progress"
android:layout_width="#dimen/_30sdp"
android:layout_height="#dimen/_30sdp"
android:indeterminateTint="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Extend the default theme and override colorAccent
<style name="AppTheme.WhiteAccent">
<item name="colorAccent">#color/white</item> <!-- Whatever color you want-->
</style>
add ContentLoadingProgressBar the android:theme attribute:
android:theme="#style/AppTheme.WhiteAccent"
You need to set both style and android:theme with accent color you want your progress bar in. Refer below code snippets:
layout.xml
<androidx.core.widget.ContentLoadingProgressBar
android:theme="#style/ContentLoadingProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
styles.xml
<style name="ContentLoadingProgress">
<item name="colorAccent">#color/colorWhite</item>
</style>

Can't change background color on MaterialButton without change colorAccent

Android Studio 3.2.1
Here my layout:
<com.google.android.material.button.MaterialButton
android:id="#+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/json_view"
app:layout_constraintBottom_toBottomOf="#+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="#+id/binanceJsonViewButton" />
to change MaterialButton's background I change colorAccent in styles.xml
<item name="colorAccent">#color/colorAccent</item>
Nice. It's work.
But the problem is: I do not want to change colorAccent. I want to use background's color for MaterialButton's different from colorAccent
Attribute:
android:background="#aabbcc"
not help.
1st Solution
You can use app:backgroundTint to change back ground color of MaterialButton
<com.google.android.material.button.MaterialButton
android:id="#+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
app:backgroundTint="#android:color/holo_orange_dark"
android:text="#string/json_view"
app:layout_constraintBottom_toBottomOf="#+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="#+id/binanceJsonViewButton" />
2nd Solution
MaterialButton uses colorPrimary as background when button is in active state and colorOnSurface when disabled. So, you can define it in your theme and apply it on material buttons
With the MaterialButton you have 2 options:
Using the backgroundTint attribute as suggest by Zaid Mirza
If you want to override some theme attributes from a default style you can use new materialThemeOverlay attribute. It is the best option in my opinion.
Something like:
<style name="Widget.App.ButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<item name="colorPrimary">#color/green</item>
</style>
and then:
<com.google.android.material.button.MaterialButton
style="Widget.App.ButtonStyle"
../>
It requires at least the version 1.1.0 of the library.
If you want to set custom drawable you need to make the app:backgroundTint="#null". For just changing the background colour app:backgroundTint="#color/yourColor"
I'm currently using 1.3.0-alpha01
<com.google.android.material.button.MaterialButton
android:id="#+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
app:backgroundTint="#null"
android:background="#drawable/your_custom_drawable"
android:text="#string/json_view"
app:layout_constraintBottom_toBottomOf="#+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="#+id/binanceJsonViewButton" />
You can do it by following the below code.
android:background="#color/black"
app:backgroundTint="#null"
2020: It seems that they just fixed this on April 1st 2020.
It should be released on 1.2.0 beta 1 since the GitHub issue was closed as
"Fixed"
The solution that worked for me is described below:
On Button tag
<Button
android:id="#+id/login_btn"
style="#style/PrimaryButtonStyle"
app:backgroundTint="#null"
android:enabled="true"
android:text="#string/txtBtnLogin" />
#Style/PrimaryButtonStyle
<style name="PrimaryButtonStyle" parent="#style/Widget.MaterialComponents.Button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:background">#drawable/base_button_style</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
</style>
Output of this - Button background (light blue) is different than layout background (Comparatively dark blue)
backgroundTint also changed the disabled state color so wasn't good for me
Best solution i could find was to override the primary color for the MaterialButton (only) through overlay style
Add this code to your styles.
Replace ?attr/colorSecondary to whatever color you want
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStyleTextColor</item>
</style>
<style name="ButtonStyleTextColor">
<item name="colorPrimary">?attr/colorSecondary</item>
</style>
Add the theme to the button
<com.google.android.material.button.MaterialButton
//..
android:theme="#style/MyButtonTheme"/>
Or
If you you using MDC and you want to change the theme for all buttons:
Add this row to your themes.xml
<item name="materialButtonStyle">#style/Button.MyTheme</item>
and add these lines to your type.xml
<style name="Button.MyTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStyleTextColor</item>
</style>
<style name="ButtonStyleTextColor">
<item name="colorPrimary">?attr/colorSecondary</item>
</style>
In that case you don't need to add android:theme="#style/MyButtonTheme" to your MaterialButton
If any mistake please let me know and don't be hurry to downgrade
BackgroundTint Always works on material buttons, but first, do uninstall the app and reinstall it again. Sometimes changes may not reflect until you reinstall the app.
android:backgroundTint is applied over the android:background and
their combination can be controlled by android:backgroundTintMode
do check this answer for difference between android:background, android:backgroundTint and android:backgroundTintMode
https://stackoverflow.com/a/38080463/14289342
Change the backgroundTintMode to add and then your background attribute will be displayed. See example below:
<com.google.android.material.button.MaterialButton
android:id="#+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/json_view"
android:background:"#aabbcc"
app:backgroundTintMode="add"
app:layout_constraintBottom_toBottomOf="#+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="#+id/binanceJsonViewButton" />
Comments asking about disable color using colorOnSurface you need to use theme settings,
Like this:
<style name="MaterialRedButton"
parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/MaterialRedButtonThemeOverlay</item>
</style>
<style name="MaterialRedButtonThemeOverlay">
<item name="colorPrimary">#android:color/holo_red_dark</item>
<item name="colorOnSurface">#color/white</item>
</style>
I had the same problem, and here is what it did:
create a new style with a parent of a style of on of the material button styles and change the background color and background tint in it.. hopefully it will work with you..
<style name="DialogMaterialButtonOkay" parent="Widget.Material3.Button.UnelevatedButton">
<item name="background">#color/button_background_color_main</item>
<item name="backgroundTint">#color/button_background_color_main</item>
</style>

ImageButton background color with appcompat

I've got problems to have a good background from an AppCompatImageButton, just to try I'm comparing this two layouts:
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/imageButtonI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:contentDescription="#string/icolor"
android:tint="#color/accent"
app:srcCompat="#drawable/magnify"/>
<ImageButton
android:id="#+id/imageButtonS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:contentDescription="#string/scolor"
android:tint="#color/accent"
android:src="#drawable/magnify"/>
and the style file:
<style name="AppBaseTheme" parent="android:Theme.Material">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primaryDark</item>
<item name="android:colorAccent">#color/accent</item>
</style>
As you can see from this image however, the background of ImageButton is "normal" while there isn't any background in the appcompat one. How can I have the "normal" background using AppCompatImageButton?
A good way to style the Button is to use the style #style/Widget.AppCompat.Button.Colored.
The Widget.AppCompat.Button.Colored style extends the Widget.AppCompat.Button style and applies automatically the accent color you selected in your app theme.
<Button
style="#style/Widget.AppCompat.Button.Colored"
/>
To customize the background color without changing the accent color in your main theme you can create a custom theme for your Button using the android:theme attribute and extending the ThemeOverlay theme.
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:theme="#style/MyButtonTheme"/>
defining the following theme:
<!-- res/values/themes.xml -->
<style name="MyButtonTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">#color/my_color</item>
</style>
I am not sure which version of support library are you using, but before there was bug in the library which is reported here,
https://code.google.com/p/android/issues/detail?id=78428
But they have resolved it in the latest update,
Yes, it appears to work now with the introduction of
android.support.v7.widget.AppCompatButton in AppCompat v22.1.0, color
can be controlled at overall theme level with "colorButtonNormal".
http://android-developers.blogspot.com/2015/04/android-support-library-221.html
http://chris.banes.me/2015/04/22/support-libraries-v22-1-0/
for theme
<item name="colorButtonNormal">#color/button_color</item>
for version 21
<item name="android:colorButtonNormal">#color/button_color</item>
Hope this will help you.
Thanks

Android Button background color not changing

In this android project im creating a default button style.
my styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">#style/ButtonStlye123</item>
</style>
<style name="ButtonStlye123" parent="android:style/Widget.Button">
<item name="android:textSize">19dp</item>
<item name="android:layout_margin">8dp</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#color/ColorDivider</item>
<item name="android:background">#color/ColorAccent</item>
</style>
my button in a fragment.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnLogin"
android:id="#+id/btn_login"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:theme="#style/ButtonStlye123"
android:onClick="login" />
In the preview in android studio it looks exactly like I want, but when I run the app on my device the background color is gray ("default button color"). If i change the text color in my styles.xml like:
<item name="android:textColor">#color/ColorPrimary</item>
it changes (also on my device) so the custom style is loading, i tryd different colors (that worked for the text) for the button but it wont change.
Why isnt my background color loading?
Try use AppCompactButton
instead of
<Button
use
<androidx.appcompat.widget.AppCompatButton
that will do the trick
Update: 01/06/2021
Found out that the above will not work on earlier Android versions.
For materiel design Button use
app:backgroundTint="#color/green"
Please Use androidx.appcompat.widget.AppCompatButton instead of Button.
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_id_Login"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#id/textInnputLayout_editText_id_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="#string/Login_screen_button_text"
android:background="#drawable/login_button_style"
android:textAllCaps="false">
</androidx.appcompat.widget.AppCompatButton>
You might be using targetSdkVersion 30 material theme
The solution is change theme.xml style
From
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
To
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
Replace with
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Changing MaterialComponents to AppCompat works for me..
I think you need to change your attribute from "theme" to "style". These files are intended for different things, so are used differently.
Style is used to assign attributes to individual views or components, and Theme is used to apply attributes to the entire app.
So try this instead:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnLogin"
android:id="#+id/btn_login"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
<!-- This attribute was changed -->
android:style="#style/ButtonStlye123"
android:onClick="login" />
You should also split these different types of XML into the correct files as appropriate (styles.xml and themes.xml - instead of keeping them all in the same file). This is convention, and won't effect the code compilation or execution, but is recommended as a code style.
Use this attribute backgroundTint it will help you. It work for me. Example :
android:backgroundTint="#color/md_orange_800"
if you are using Android Studio Version 4.1 and above (Beta)
check your themes.xml file in values.
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
</style>
change the attribute to #null
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#null</item> <!-- HERE -->
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
</style>
for other old versions of Android Studio
just change this attribute
<item name="colorPrimary">#color/colorPrimary</item>
to
<item name="colorPrimary">#color/#null</item>
finally set the background of the button to the drawable icon you want.
example:
<Button
android:id="#+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_launcher_background" /> <!--the drawable you want-->
You only need to change your App Theme from Theme.MaterialComponents.DayNight.DarkActionBar to Theme.AppCompat.Light.NoActionBar inside styles.xml file
example :
from : style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"
To : style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
your style should be like this....
<style name="ButtonStlye123" >
<item name="android:textSize">19dp</item>
<item name="android:layout_margin">8dp</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#color/ColorDivider</item>
<item name="android:background">#color/ColorAccent</item>
</style>
and in layout
<Button
android:id="#+id/btn3"
style="#style/ButtonStlye123"
android:layout_width="#dimen/sixzero"
android:layout_height="#dimen/sixzero"
android:text="3" />
I am using targetSdk 30 and compileSdk 32 and I am using Button widget. Android Studio version 2021.1.1
In my case, following change worked,
In AndroidManifest.xml, under "application" tag changing "android:theme" from
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
to
android:theme="#style/Theme.MaterialComponents.DayNight.NoActionBar"
I moved to Theme.AppCompat.Light.NoActionBar to remove the title bar. At first I tried to change "style" to "Theme.AppCompat.Light.NoActionBar" inside themes.xml but it didn't work for me.

Categories

Resources