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.
Background drawable not applying to Android button
You can't apply the background on the material button.
If you are using Theme.MaterialComponents.*
, then the normal button also used as the material button forcefully.
You have two options.
- change it to any AppCompat theme.
- Still If you need a material theme for some components, then use any material theme with a bridge. like
Theme.MaterialComponents.*.Bridge
So, If you will go with option 2, you can use material button as per your need.
use com.google.android.material.button.MaterialButton
for material button else use your normal button.
Button background color is not showing in my android activity
as indicated by commonsware in his comment, the answer is in using the backgroundTint
instead of the backgroundColor
in the Style.xml. or changing 'button' tag into 'android.widget.Button'.
Because since Android Studio 4.1 any Button
elements in a layout get turned into MaterialButton
widgets, which ignores background attribute.
for more details refer to commonsware answer
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.
Gradient color not applied to button in android studio
it is getting overwritten by the colors in themes.xml file. Go to your themes.xml file (in res/values/themes) and replace
<item name="colorPrimary">@color/purple_500</item>
with
<item name="colorPrimary">@null</item>
to see your gradient work.
EDIT: I'm not sure why this is... could be a bug in newer android studio versions? But I was able to get your button gradient working with this solution
Android Studio 4.1 bordered Button loses border setting background color in kotlin code
I solved it with
punktelinks.setBackgroundResource(R.drawable.shapecolory)
in code and with resource
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFF00" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
This works (for me)
Android Button background is not recognizing/using the drawable
You have to add Width
and Height
to your Shape
in
btnbackground.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#FF5256AC"
android:endColor="#FF662D91"
android:startColor="#FF29ABE2"
android:type="linear" />
<corners android:radius="150dp">
</corners>
If you don't use AndroidX, you have to change your Theme from Theme.MaterialComponents.Light.NoActionBar
to Theme.AppCompat.Light.NoActionBar
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">@android:color/black</item>
</style>
<style
name="SplashTheme"
parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ButtonStyle" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:textColor">#fff</item>
<item name="android:background">#000</item>
</style>
<style
name="SecondTheme"
parent="AppTheme">
<item name="android:windowFullscreen">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
Button
<Button
android:id="@+id/buttonLogin"
android:layout_width="160dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Anmelden"
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="16sp"
android:theme="@style/ButtonStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
here's the result :
Cannot change XML button background color as expected
This may solve the problem
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/comma"
android:layout_width="0px"
android:layout_height="90dp"
android:layout_weight="9"
android:background="@drawable/button_border"
android:insetTop="0dp"
android:insetBottom="0dp"
android:text="."
android:textColor="#7ff5f2"
android:textSize="120px" />
/>
Notice the change in the first line.
Related Topics
Android Manifest Restrict to Tablets
Authentication Using Facebook at First and Then Google Causes an Error in Firebase for Android
Android Searchview Filter Listview
How to Write a Drawable Resource to a File
How to Disable Facebook Single Sign on for Android - Facebook-Android-Sdk
Save Data and Change Orientation
Ssl Certificate Is Not Trusted - on Mobile Only
Adb Remount Permission Denied, But Able to Access Super User in Shell -- Android
Android Alarmmanager Not Waking Phone Up
How to Show and Hide Actionbar with Appcompat V.7
Android: Fragments Overlapping Issue
Android: Programmatically Adding Buttons to a Layout
How to Keep a Countdowntimer Running Even If the App Is Closed