0

Ive had a look at this question but as of the time i typed this question, there aren't any answers with Kotlin code.

in my colours.xml file how would i access colours like these for example using a string?

<resources>
    <!-- Orange -->
    <color name="orangePrimary">#f6a02d</color>
    <color name="orange1">#e3952a</color>
    <color name="orange2">#da8f28</color>
    <color name="orange3">#d08926</color>
</resources>

The Java version is apparently this

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

when android studio translates the code i get this

val desiredColour: Int = getResources().getColor(
                                    getResources().getIdentifier(
                                        "my_color",
                                        "color",
                                        getPackageName()
                                    )

However packageName() and getResources() turn out red for some reason which means there is an error

So what would the Kotlin version be?

2
  • 1
    You can copy-paste Java code into a Kotlin file, and the IDE will automatically convert it to Kotlin for you. Commented Dec 25, 2020 at 3:45
  • When i use the code above i get the following translation which has red coloured code, meaning errors or code with a lot of different libraries to choose from, i dot know whats the right one.. val desiredColour: Int = getResources().getColor( getResources().getIdentifier( "my_color", "color", getPackageName() ) ) Commented Dec 25, 2020 at 4:29

2 Answers 2

1

There is only one possible explanation for this. getPackageName() and getResources() are not present where you are pasting the code.

For example if I paste your code in an activity, everything seems good.

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName))

with theme:

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName),theme)

But if I paste it inside fragment, I need to reference the activity of that fragment to get the package name.

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName))

with theme:

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName),activity?.theme)

And if for some reason you are pasting it elsewhere besides activity or fragment, you need to pass context or activity to call those method.

object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName))
}

with theme:

object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName), context.theme)
}
Sign up to request clarification or add additional context in comments.

6 Comments

My other problem now is that getColor() is deprecated.
@Jevon That is not a problem. Don't use the deprecated method. Instead, use the updated getColor method by providing the theme. Providing a theme shouldn't be difficult. Check my updated answer.
When i do the updated version i then get this error Call requires API level 23 (current min is 14): android.content.res.Resources#getColor by the way, i need a min 14.
@Jevon Yes, the overloaded getColor method with the theme was introduced much later. To use the updated method you need to update your min sdk version or make a backward compatible method to get color based on the running sdk version of the user.
Use ResourcesCompat.getColor instead (developer.android.com/reference/androidx/core/content/res/…) - there's a bunch of ThingCompat libraries to help your code run on a range of API levels
|
0

You can set:

snackBar_View.setBackgroundColor(context.resources.getColor(com.example.app.R.color.colorPrimary))

Instead of:

snackBar_View.setBackgroundColor(context.getResources().getColor(R.color.redish))
 

Comments

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.