3

I have created a gradient_file.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp" />

    <gradient
        android:startColor="@color/white"
        android:endColor="@color/black"
        android:angle="45"
        android:type="linear" />
</shape>

I have applied it to button in my layout

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:background="@drawable/gradient_file"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.243" />

This is the result i am getting:

enter image description here

The color should be different. i have rebuild the project but the color does not change in preview nor in emulator.

marko-36
  • 1,309
  • 3
  • 23
  • 38
M.Ahmed
  • 195
  • 1
  • 3
  • 13
  • Does this answer your question? [How to add a gradient to buttons in android through xml?](https://stackoverflow.com/questions/12166559/how-to-add-a-gradient-to-buttons-in-android-through-xml) – Vatsal kesarwani Jan 12 '21 at 14:28
  • No, As you can see i am already doing what your solution is doing but i am not getting any result. The color of button is not changing – M.Ahmed Jan 12 '21 at 14:36
  • please share me the style and theme used in the project – Vatsal kesarwani Jan 12 '21 at 14:41
  • Does this answer your question? [Android Background Drawable Not Working in Button Since Android Studio 4.1](https://stackoverflow.com/questions/64722733/android-background-drawable-not-working-in-button-since-android-studio-4-1) – Sam Chen Mar 21 '22 at 20:51

4 Answers4

5

In themes.xml, change

<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

to

<style name="Theme.YourApp" parent="Theme.AppCompat.DayNight.DarkActionBar">

Then the submitted drawable will work!

Antoine
  • 1,393
  • 4
  • 20
  • 26
Ameya Pawle
  • 51
  • 1
  • 1
4

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

Tommy Bailey
  • 113
  • 6
  • Thanks it worked. Yes you are right. My color scheme should override the scheme provided by default. – M.Ahmed Jan 12 '21 at 15:15
  • Can you tell me another way which wont require my to change my theme and have this work? – M.Ahmed Jan 12 '21 at 15:20
  • Because the views that use that primary-color will lose that color. so it there another way? – M.Ahmed Jan 12 '21 at 15:44
  • @M.Ahmed - I wish I knew. Like I mentioned I'm not sure why this is happening and seems to be an issue in newer versions of android studio from what I can tell. – Tommy Bailey Jan 12 '21 at 16:13
3

I resolved this issue like this in button attributes:

app:backgroundTint="@null"
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Abbas Raza
  • 31
  • 1
0

I also resolved this issue by using this. Go to themes.xml and change color primary by this, @null

Kinza
  • 1
  • 1