XML : Android - How to change only the background color of a button and keep the rest of button default style?

I have looked at pretty much every answer here and I can't find something that changes only the background color of my button but keeps all the rest of the Button default values.

I have tried using the PorterDuff solution many answers suggested:

  button.getBackground().setColorFilter(0x39B44A, PorterDuff.Mode.MULTIPLY);    

but it made the green color I wanted a very dark green color for some reason.

So I tried making a drawable file solution as was suggested by that high voted answer:

  <?xml version="1.0" encoding="utf-8"?>  <selector xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_pressed="true" >          <shape>              <gradient                  android:startColor="@color/green"                  android:endColor="@color/green"                  android:angle="270" />              <stroke                  android:width="3dp"                  android:color="@color/green" />              <corners                  android:radius="3dp" />              <padding                  android:left="10dp"                  android:top="10dp"                  android:right="10dp"                  android:bottom="10dp" />          </shape>      </item>        <item android:state_focused="true" >          <shape>              <gradient                  android:endColor="@color/green_lightest"                  android:startColor="@color/green_lighter"                  android:angle="270" />              <stroke                  android:width="3dp"                  android:color="@color/green_lighter" />              <corners                  android:radius="3dp" />              <padding                  android:left="10dp"                  android:top="10dp"                  android:right="10dp"                  android:bottom="10dp" />          </shape>      </item>        <item>          <shape>              <gradient                  android:endColor="@color/green"                  android:startColor="@color/green"                  android:angle="270" />              <stroke                  android:width="5dp"                  android:color="@color/blue"/>              <corners                  android:radius="5dp" />              <padding                  android:left="0dp"                  android:top="0dp"                  android:right="0dp"                  android:bottom="0dp"/>              <solid                  android:color="@color/green"/>            </shape>      </item>    </selector>    

But this solution doesn't work for me because I'm using other default looking buttons so this solution has me eyeballing the look of my custom button by having to experiment with different dp values in my custom button drawable file to match the default look. Here's an image of what I was getting (the Sign Up button is using the default button style). I was able to get the green I wanted but now there's a weird black border around it, which I have no idea how to remove.

enter image description here

So the next thing I tried was looking for the default values that are given to the default Button style. Searching through multiple files, I finally found the drawable file that gave the default Button look and it was a .9.png image file. I couldn't find any details on what the default values were online or in any of the drawbale files. I just simply want to do what android:backgroundTint="@color/green" would do. But I'm supporting API 16 and up so I can't use it.

This is very frustrating just to change the background color but keep the default style. Any help would be much appreciated.

No comments:

Post a Comment