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.
android-button background drawable doesn't work
Try out selector as below:
<!-- Active tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_pressed="true"/>
<!-- Selected tab (using d-pad) -->
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
</selector>
For more detail guidenc check out Custom Selector in Android
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 :
android:background=@drawable/XXXX is ignored for a button in main.xml
Changing background is not ignored. Simply background is not visible because of the foreground drawable. You may find more info here: Standard Android Button with a different color
or ready to use sample here: http://ogrelab.ikratko.com/custom-color-buttons-for-android/
Setting Background Drawable to Button in xml does not change the color Android Studio 4.2 preview
In Material Design components, buttons have a default backgroundTint value set to colorPrimary that is why no matter what color you use in your button's background, it will be tinted blue(colorPrimary).
Just Add this line in the button's code : app:backgroundTint="@null"
And a better way to make your buttons round in material design:
In your themes.xml file add this :
<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
</style>
Then if you want to apply round corners to all buttons in the app, add this line in your app's main theme :
<item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>
Otherwise, if you want to apply this style to specific button, add this line to it's code:
style="@style/Widget.App.Button"
Related Topics
How to Use Adb With Emulator Like Memu/Bluestacks/Nox App Player
Unable to Add Window - Token Null Is Not Valid; Is Your Activity Running
How to Send a Firebase Notification to All Devices Via Curl
Setting Webview to View Desktop Site and Not Mobile Site
Move to Another Edittext When Soft Keyboard Next Is Clicked on Android
How to Show a Video Thumbnail from a Video Path
Resolving Problems With Android Studio Freezing Constantly
Missing Android Platform(S) Detected
How to Dynamically Hide a Menu Item in Bottomnavigationview
Retrofit @Body With @Multipart Having Issue
Where Is Adb.Exe in Windows 10 Located
How to Show List of String in Textview on Android
How to Make Primary Key as Autoincrement for Room Persistence Lib
How to Set the Starting Position of a Scrollview
Android Emulator Is Start But Window Show Blank Screen
Android: How to Open a Specific Folder Via Intent and Show Its Content in a File Browser
How to Capitalize the First Letter of Text in a Textview in an Android Application