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?

6
  • Use setBackGroundRes() method . Commented Jan 22, 2017 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 Commented Jan 22, 2017 at 11:53
  • stackoverflow.com/questions/17823451/… Commented Jan 22, 2017 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... Commented Jan 22, 2017 at 12:15
  • 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. Commented Jan 22, 2017 at 12:47

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I finally managed to solve the problem by myself... Anyway, thanks for the help;)
than post your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.