Home > Back-end >  I am trying to design a custom button in android studio, but it isn't getting applied?
I am trying to design a custom button in android studio, but it isn't getting applied?

Time:12-29

I have worked with android studio previously also, and have designed custom buttons and it use to get applied but recently I am getting issue that the colors are not being applied, shape and corner radius get applied but color doesn't, it remains purple.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <corners android:radius="20dp"></corners>

    <solid android:color="#11fa30"></solid>
</shape>

I have saved this file in drawable folder. also I have applied it to the background of the button

 <Button
                    android:id="@ id/stop_rec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textStyle="bold"
                    android:layout_margin="1dp"
                    android:background="@drawable/button_pressed"
                    android:text="@string/stop_r" /> 

shape is being applied but not the color. Can someone please guide why am I facing this issue.

CodePudding user response:

Add this line to your button's xml code :

app:backgroundTint="@null"

CodePudding user response:

So i just experimented with some things the other day and i could only change the button background AND background color by using androidx.appcompat.widget.AppCompatButton instead of the normla Button. This way you can keep everything as it is and only change the tag as below:

 <androidx.appcompat.widget.AppCompatButton
           android:id="@ id/stop_rec"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:textStyle="bold"
           android:layout_margin="1dp"
           android:background="@drawable/button_pressed"
           android:text="@string/stop_r" /> 

I would recommend that you use a style for easier reusability and a selector drawable to manage the "button-pressed" state ;)

If you want to use the shape for other views as well, but with another color, you can add background-tint with your color below the android:background="@drawable/button_pressed".

I also noticed that you have a layout_weight without changing the width or height to 0. Maybe you forgot to delete that

Hope i could help :)

CodePudding user response:

encode the file using UTF-8, if it doesn't work change the color value in colors.xml

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

<corners android:radius="20dp"></corners>

<solid android:color="#11fa30"></solid>
</shape>
  • Related