Search code examples
androidandroid-layout

Android Studio custom button color is not changing while shape is changing


I added a custom button, changed some values for every state but still when I press the button the button is not changing, and just getting the default style, I changed the shape in form rectangle to oval so I can see if in selector does it enter to the pressed state and I see it does enter now the only problem is the solid or the color that its not changing.

custom button implementation:

<?xml version="1.0" encoding="utf-8"?>
<item android:state_enabled="false">
    <shape android:shape="rectangle">
        <solid android:color="@color/LightGray"/>
        <corners android:radius="200dp"/>
    </shape>
</item>

<item android:state_pressed="true">
    <color android:color="@color/Blue"/>
    <shape android:shape="rectangle">
        <corners android:radius="200dp"/>
    </shape>
</item>

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/DarkBlue"/>
        <corners android:radius="200dp"/>
    </shape>
</item>

and Button implementation:

  <Button
    android:id="@+id/btnNext"
    android:layout_width="102dp"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="68dp"
    android:text="Next >"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
   />

Solution

  • I figured out that when using the custom button make sure to change the

    <Button.../>
    

    to

    <androidx.appcompat.widget.AppCompatButton
    

    It's due to material io buttons can't accept custom buttons as background.