TheAmazingKnight
TheAmazingKnight

Reputation: 2474

How do I pass the value of an array to another class?

I'm trying to pass in an array value of colors that is in another class to FunFactsActivity in Android Studio. Right now, I'm getting an error
setBackgroundColor(int) in View cannot be applied to (java.lang.String). relativeLayout.setBackgroundColor(mColorWheel.mColors[currentColor]);

From what I understand, I can't pass in an int into it since its a String, but I'm just trying to make the colors i already have in that array fade every few seconds/intervals to the relativeLayout background in a thread and can't get it right. What am I doing wrong?

FunFactsActivity.java

// update background color of relativeLayout every few seconds.
private void updateColor()
{
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    int color = mColorWheel.getColor();
    runOnUiThread(new Runnable()
    {

        @Override
        public void run()
        {

            if (currentColor > mColorWheel.mColors.length - 1)
            {
                currentColor = 0;

            }
            relativeLayout.setBackgroundColor(mColorWheel.mColors[currentColor]);

            currentColor++;

        }// end of run
    });

}

ColorWheel.java

public class ColorWheel {

    // Member variable (properties about the object)
    public String[] mColors = {
            "#39add1",
            "#3079ab",
            "#c25975",
            "#e15258",
            "#f9845b",
            "#838cc7",
            "#7d669e",
            "#53bbb4",
            "#e0ab18",
            "#637a91",
            "#f092b0",
            "#b7c0c7",
            "#FAEBD7",
            "#00FFFF",
            "#7FFFD4",
            "#0000FF",
            "#8A2BE2",
            "#A52A2A",
            "#DEB887",
            "#5F9EA0",
            "#7FFF00",
            "#D2691E",
            "#6495ED",
            "#DC143C",
            "#B8860B",
            "#A9A9A9",
            "#006400",
            "#FF8C00",
            "#8B0000",
            "#FFD700",
            "#FF69B4",
            "#4B0082",
            "#F08080",
            "#90EE90",
            "#87CEFA",
            "#FF4500",
            "#DA70D6",
            "#FA8072",
            "#9ACD32",
            "#00FF7F",
            "#4682B4",
    };

    // Method (abilities: things the object can do)
    public int getColor(){

        String color = "";
        // Randomly select a fact
        Random randomGenerator = new Random(); // Construct a new Random number generator
        int randomNumber = randomGenerator.nextInt(mColors.length);

        color = mColors[randomNumber];
        int colorAsInt = Color.parseColor(color);

        return colorAsInt;
    }
}

Upvotes: 0

Views: 97

Answers (2)

Christian Abella
Christian Abella

Reputation: 5797

Your ColorWheel class is already calling Color.parseColor in getColor function. Just use the function as it is getting a random color already.

relativeLayout.setBackgroundColor(mColorWheel.getColor());

or if you want to use the mColors array:

relativeLayout.setBackgroundColor(Color.parseColor(mColorWheel.mColors[currentColor]));

Upvotes: 2

Ivan Skoric
Ivan Skoric

Reputation: 1210

Didn't go through the whole code, but when setting the color in Android, you either:

  • provide an int which is a reference to the color id, for example: R.color.my_red
  • provide a Color object. In your case if you want to convert hex value to a Color, call Color.parseColor("#000000")

Upvotes: 1

Related Questions