0

I need to change my application's colors during runtime. I parse data file to get colors and I save it in class with static fields and methods:

public class Colors {

    private static String colorOneBackground = "#00577F";
    private static String colorOneForeground = "#FFFFFF";

    public static void setColorOneBackground(String colorOneBackground) {
        Colors.colorOneBackground = colorOneBackground;
    }

    public static int getColorOneForeground() {
        return Color.parseColor(colorOneForeground);
    }
    // more colors...

Then, for example when I want to change the background of screen I do it so:

RelativeLayout relativeLayout = (RelativeLayout) myView.findViewById(R.id.loginBackground);
        relativeLayout.setBackgroundColor(Colors.getColorOneBackground());

Same with textviews and other widgets. However, I have encountered one problem. Some styles are defined in Drawable folder, for example, mybutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android "
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFFFF"
        android:centerColor="#FFFFFF"
        android:endColor="#FFFFFF"
        android:angle="270" />
    <corners android:radius="5dp" />
    <stroke android:width="3px" android:color="#000000" />
</shape>

And I set this as my button's background:

<Button  
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/title"
   android:background="@drawable/mybutton" />

As I said I want to change these color values programatically. So I want to know if it is possible to dinamically change color values defined in xml file?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Naujas Tewas
  • 73
  • 1
  • 9
  • Use **setBackGroundRes()** method . – aslamhossin Jan 22 '17 at 11:50
  • Please read my question once again. I want to change color values in xml file and I know how to set drawable background in code – Naujas Tewas Jan 22 '17 at 11:53
  • http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically – amorenew Jan 22 '17 at 12:04
  • In your link the color is being changed using this approach: GradientDrawable gradientDrawable = (GradientDrawable) background; gradientDrawable.setColor(getResources().getColor(R.color.colorToSet)); but in my xml file I may have different colors for startColor, centerColor and endColor so setColor() mehod won't work... – Naujas Tewas Jan 22 '17 at 12:15
  • so use `setColors()` method – pskink Jan 22 '17 at 12:24
  • 1
    Doing the above is unnecessary and just complicates your code..You don't **need** to redefine the colors in xml files.. Just define the different colors used in your application inside the `colors.xml` and use the methods specified in the previous comments to set them. – ashkhn Jan 22 '17 at 12:47

1 Answers1

3

try this : to change color in your drawable xml file:

button.setBackgroundResource(R.drawable.mybutton);  //drawable id
GradientDrawable gd = (GradientDrawable) button.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000")); //set color
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62