Szymon
Szymon

Reputation: 165

Gradient in xml doesn't occur on a button

I created a gradient in my Drawable Resource File (.xml) and then connected it to button's (in activity_main.xml) background. Unfortunately, all structures in this file didn't occur on this button. I am a beginner. Is this problem with the code or something else? The gradient works as an ImageView and even as a Switch but does not work as a button's background. Code from gradient.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <item>
        <shape>
              <gradient
                   android:angle="90"
                   android:endColor="#b5b6d2"
                   android:startColor="#555994"
                   android:type="linear" />
               <corners
                   android:radius="0dp"/>
               <padding
                   android:right="10dp"
                   android:top="10dp"
                   android:bottom="10dp"
                   android:left="10dp"/>
        </shape>
    </item>
</selector>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/gradient"
        android:text="@string/button"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 466

Answers (2)

Medha
Medha

Reputation: 261

You use selector when you want to show different background / color according to different states of the widget . For your case replace your code with this code in your drawable file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
<gradient
    android:angle="90"
    android:endColor="#b5b6d2"
    android:startColor="#555994"
    android:type="linear" />
<corners
    android:radius="0dp"/>

</shape>

Correction : The Button widget is taking colorAccent as default background strangely change the widget to AppCompatButton and it will solve the problem .

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Button"
        android:background="@drawable/gradient"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

selectors are used to change the view when states change. You haven't supplied any states in your selector, so that's probably why it doesn't get applied.

If you want the gradient applied in all states, then remove the selector and item in the xml, and just provide the shape.

Upvotes: 0

Related Questions