androidbuttonoval

Change oval shaped button background programmatically


I want to change Button background color programmatically the button shape(oval). I have changed in xml.

This is the code:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="20dp"/>  
        <solid android:color="#f9f9f9"/>  
        <stroke
            android:width="2dp" android:color="#FFFF4917" /> 
            </shape>

I am using Button.backgroundColor(COLOR.RED) to change this Button but problem is that it's changing button color, but it is making my button rectangular shape(default shape of a button)


Solution

  • You could modify it using this simple method down below

    GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground();
    BackgroundShape.setColor(Color.BLACK);