Why is my button taking colorPrimary as the default background color?
Use app:backgroundTint
in your layout XML. It will change the color of the background from the primary color of the app
is there anyway to change background image (not color) of button from xml in android studio 4.2.2?
Try this code
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_background"
android:text="@string/ok"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_cancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.3"/>
Android Button background color not changing
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.
Android material button taking color primary instead of color accent
You can check the official doc.
The filled button has the backgroundTint
based on the colorPrimary
.
Also in your buttonView
style you should extend one of the provided styles:
Default style Widget.MaterialComponents.Button
Icon style Widget.MaterialComponents.Button.Icon
Unelevated style. Widget.MaterialComponents.Button.UnelevatedButton
Unelevated icon style Widget.MaterialComponents.Button.UnelevatedButton.Icon
Example:
<style name="buttonView" parent="Widget.MaterialComponents.Button">
</style>
If you want to change the background color you can:
Use the
backgroundTint
attribute in your custom styleOverride the
colorPrimary
attribute in your custom style using thematerialThemeOverlay
(best solution)
Example:
<style name="buttonView"parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">@style/CustomButtonThemeOverlay</item>
</style>
<style name="CustomButtonThemeOverlay">
<item name="colorPrimary">@color/...</item>
</style>
Android button background color
If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property
android:backgroundTint="@android:color/holo_green_light"
Given button color not showing but showing a color in colors file in android studio
As you shared your app theme parent is Theme.MaterialComponents.DayNight.DarkActionBar
so Your button behave like, MaterialButton so please set backgroundTint as "null"
Android Background Drawable Not Working in Button Since Android Studio 4.1
The Android Studio 4.1 new-project wizard, for many of its templates, has the project use the Material Components for Android library. And, it sets up the default theme to be based on Theme.MaterialComponents.DayNight.DarkActionBar
.
A side effect of this is that any <Button>
elements in a layout get turned into MaterialButton
widgets, not regular Button
widgets. MaterialButton
ignores android:background
.
If all you want to do is to change the color, use android:backgroundTint
or change the colorPrimary
attribute in the theme.
If you want a button that has a custom background, and your theme is set up to use Theme.MaterialComponents
, you could switch the XML element in the layout to be <android.widget.Button>
instead of <Button>
. This should cause the Material Components for Android to ignore that element, and you can manipulate this button normally with respect to XML attributes.
Related Topics
How Disable Redirect in Webview
Removing Backgroundcolor of a View in Android
Android: Rotate Image in Imageview by an Angle
Android Gradle Error When Sync Style Attribute Not Found
Android Volley Basicnetwork.Performrequest() Unexpected Response Code 404
Gradle Error: Could Not Execute Build Using Gradle Distribution
How to Fix Attempt to Invoke Interface Method on a Null Object Reference
How to Start Android Application on Device Boot
How to Show the Number Keyboard on an Edittext in Android
Decrease Image Size Without Losing Its Quality in Android
Adb Error (Error: Protocol Fault (Couldn't Read Status): Invalid Argument)
Disable Ssl as a Protocol in Httpsurlconnection
Recyclerview Is Cutting Off the Last Item
Android Adb Devices Does Not Detect My Phone