0

API 22. I want to set a border radius for my button, so I did this: How to make the corners of a button round? (I tried out the answer that is ticked as correct). How to change the color of a button? None of the methods here work either. I used to set the background color of my button with android:backgroundTint. That was the only method working. Unfortunately, when I link the file to my button via android:background="@drawable/roundbutton", the color that I selected via android:backgroundTint wont work. As well as the solid color I defined int the drawable/roundbutton. Strangely the border Radius works: My Code in activity_main.xml:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signinotherbutton"
    android:background="@drawable/roundbutton"
    android:backgroundTint="@color/grey"
    android:textColor="@color/white"
    android:textAllCaps="false"/>

My Code for the drawable/roundbutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
</selector>

Result: Button with the Radius I defined, but with the default color (ca. purple) No, @color/grey is definitely grey, not purple. Thanks for all answers

2 Answers2

0

This is my code for the rounded button with the green color you can modify it as per your convenience. Hope this will work.

button_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#57c885"/>
    <corners
        android:radius="25dp"/>
</shape> 
iamdhavalparmar
  • 1,090
  • 6
  • 23
0

I was able to solve my problem by creating a new style in the themes.xml and setting it as the theme of my button. Heres my code in the themes.xml:

<style name="Theme.ButtonWhite" parent="Base.Widget.AppCompat.Button.Colored">
    <item name="colorPrimary">@color/white</item>
</style>

Code for the button in activity_main.xml:

android:theme="@style/Theme.ButtonWhite"

Im really asking myself why nobody else seems to have this problem as I had the exact same problem when creating a completely new project