I am currently working on a project in android studio. In that app there are buttons in all activities. In the MainActivity
(It is main menu of app) there are 3 buttons. I created a drawable file named rounded. It is used for the background of buttons. The following codes are in the rounded
file. This will turn the buttons to round buttons.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#9AFD36"
android:centerColor="#8CE63E"
android:endColor="@color/teal_700"
android:angle="180"
android:type="linear"/>
<corners android:radius="10000dp"/>
</shape>
I added this background to buttons like this,
<Button
android:id="@+id/areabtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Surface Area"
android:layout_centerInParent="true"
android:background="@drawable/rounded"
android:textColor="@color/black"/>
In the 7th line I set background to that file and the buttons will show up that gradient there but when I run it, It turns to the Primary Color in the colors file. The following colors are in the colors file,(The primary color is purple_500
)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#11CC6B</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
This is in the code in themes
file in the values folder.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MathematiciansCalculator" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Also I have another gradient set to the back button in other activities but also they don’t show the background given.
Android Studio Version :- Android Studio Bumblebee | 2021.1.1 Canary 11
4
Answers
I found the answer for my question with the help of @KGeeks. As she told it needs
app:backgroundTint="@null"
line.Use MaterialButton for round corners
As you shared your app theme parent is Theme.MaterialComponents.DayNight.DarkActionBar
so Your button behave like, MaterialButton so please set backgroundTint as "null"
Hello @RedDragon the point of the existing answers are good but not complete or solving your question properly in my opinion:
app:backgroundTint="@null"
will disable the existing background color of theMaterialButton
or change it if you put a value instead ofnull
.Hint: you can also use
app:background="@empty"
But the important point to mention is that
MaterialButton
doesn’t support gradients, only single colors.A short field trip to why Tint instead of
android:background
from the documentation:As you can see that is very different from the standard widgets in Android,
MaterialDesign
is a library.Now to the solutions of using gradients with a Button is following:
Make use of
androidx.appcompat.widget.AppCompatButton
if you are using Material Theme to be able to apply background gradients, becauseMaterialButton
doesn’t offer support to it yet as said before. There you can set yourdrawable
asandroid:background
attribute or with@style
.