1

I am retrieving football data from an API, and part of this data is the 'club colours'. The colours are arranged in a string like 'colour1 / colour2 / colour3' and so on. I am able to split this string into the separate colours with the following code

String[] splitClubColors = clubColors.split("/");
for(int i = 0; i < splitClubColors.length; i++) {
    splitClubColors[i] = splitClubColors[i].trim().toLowerCase();
}

So now I have each colour as a separate string, for instance 'red', 'white' and 'black'.

I have two separate questions:

1) is it possible to convert a colour from its name form (e.g. 'blue') to a form that is useful in Android Studio (i.e. its hex code).

2) and is it possible to set the colorPrimary, colorPrimaryDark and colorAccent using these colours once they have been converted

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

is it possible to convert a colour from its name form (e.g. 'blue') to a form that is useful in Android Studio (i.e. its hex code).

int color = getResources().getColor(getResources().getIdentifier("red", "color", getPackageName()));

Note: red is a color placeholder defined in color.xml file

is it possible to set the colorPrimary, colorPrimaryDark and colorAccent using these colours once they have been converted

No, you cannot although you can change the colour of some window's widgets at runtime

Reference:

How to really programmatically change primary and accent color in Android Lollipop?

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

Solution:

1) Refer to this

2) Refer to this

Ishaan Kumar
  • 968
  • 1
  • 11
  • 24