How do I pass R.drawable.XXX value to a function in kotlin?

114
May 10, 2022, at 1:20 PM

Here's my code:

private fun roomChange(animation: Int)
{
    val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
        setBackgroundResource(R.drawable.animation)
        foxanim = background as AnimationDrawable
        foxanim.start()
    }
}

I have anim.xml file and would like to pass it to a function but "animation" is an unresolved reference. How would I go about passing it correctly? Thanks in advance!

Answer 1

Here's the enum thing I mentioned in the comments:

// Basic enum with an animation resource property - add one for each anim
// The @AnimRes annotation is optional, just gives you a warning if you use something else
enum class StateAnimation(@AnimRes val animationId: Int) {
    ANIM(R.drawable.animation),
    RUN(R.drawable.run)
}
...
// set your function to take one of your animation definitions instead
private fun roomChange(stateAnim: StateAnimation) {
    // pull out the resource ID from the passed animation
    setBackgroundResource(stateAnim.animationId)
}
// call your function using the animation you want
roomChange(StateAnimation.RUN)

The idea here is you can create an enum class that defines all your animations, so you can name each one however you like, and they all have a resource ID for the animation they represent. Then you pass one of those to your function instead, and the function can refer to its resource ID

This way you get code completion and type checking (you have to pass one of the StateAnimations, not any other animation ID, resource ID, or random int). Your code can be cleaner because StateAnimation.RUN is pretty clear (you might want to rename that but you get the idea), and it's safer than passing a string like "run" and converting that into a resource lookup which may or may not exist

And if you want, you can just import everything in StateAnimation (put your cursor on RUN and do Alt+Enter or click the lightbulb and it'll offer to do it for you) and then you can just call roomChange(RUN) and it's nice and concise!

Answer 2

R.drawable.whatever is just an Int. That's all you need to pass along to setBackgroundResource().

If you want the IDE to help prevent you from accidentally passing the wrong kind of argument, you can also add @AnimRes. Then in certain cases when it can detect that you are passing an Int that is not an animation resource ID, it will show an error.

private fun roomChange(@AnimRes animation: Int)
{
    val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
        setBackgroundResource(animation)
        foxanim = background as AnimationDrawable
        foxanim.start()
    }
}`
Answer 3

Modify your code like this :

private fun roomChange(animation: Drawable) {
    val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
        background = animation
        foxanim = background as AnimationDrawable
        foxanim.start()
    }
}
Rent Charter Buses Company
READ ALSO
Android multi module project

Android multi module project

I'm trying to setup a multi-module project with bottom navigation fragments and hiltIs there any github project I can use to start my project faster?

144
Add AndroidTest build type for all flavors in whitelabel app

Add AndroidTest build type for all flavors in whitelabel app

I have a whitelabel Android app, that uses flavors to build the different variantsI have setup the application so that all variants are built with fastlane, and everything works correctly except when it comes to AndroidTest, and its usage to generate...

140
IOS problem to discover services when using flutter_blue package

IOS problem to discover services when using flutter_blue package

I'm working on a project that requires the app I am developing with Flutter to connect to the Raspberry pi via Bluetooth and be able to send and receive data

77
How can I properly initialize my Android emulator using AVD?

How can I properly initialize my Android emulator using AVD?

I'm trying to run an Android emulator from command-line because I couldn't run it from Android Studio (it gave the same problem plus some error messages such as "Time out after 300seconds waiting for emulator to come online" and "The emulator...

131