I am trying to set the background color of a button in my app and I am unable to achieve the result that I want...
The color that I am trying to set is holo_gr
If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property
android:backgroundTint="@android:color/holo_green_light"
No need to be that hardcore.
Try this :
YourButtonObject.setBackground(0xff99cc00);
Just use a MaterialButton
and the app:backgroundTint
attribute:
<MaterialButton
app:backgroundTint="@color/my_color_selector"
In addition to Mark Proctor's answer:
If you want to keep the default styling, but have a conditional coloring on the button, just set the backgroundTint
property like so:
android:backgroundTint="@drawable/styles_mybutton"
Create the associated file /res/drawable/styles_mybutton.xml, then use the following template and change the colors as per your tastes:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Disabled state-->
<item android:state_enabled="false"
android:color="@android:color/white">
</item>
<!-- Default state-->
<item
android:color="#cfc">
</item>
</selector>
Create /res/drawable/button.xml
with the following content :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#90EE90"/>
<corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
</shape>
And then you can use the following :
<Button
android:id="@+id/button_save_prefs"
android:text="@string/save"
android:background="@drawable/button"/>
This is my way to do custom Button with different color.`
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#90150517"
android:endColor="#90150517"
android:startColor="#90150517" />
</shape>
This way you set as background.
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginBottom="25dp"
android:layout_centerInParent="true"
android:background="@drawable/button"/>