Search code examples
androidandroid-studiokotlinimageviewandroid-imageview

I need a way to change the image displayed by an ImageView in Android Studio (in Kotlin)


I need to change an ImageView in Android Studio to display a different image (while the program is running). I didn't find a way to accomplish this in Kotlin yet. Thanks for your time.


Solution

  • You need a reference to the ImageView (for example, using ViewBinding or findViewById), then you can use the method setImageResource, as stated in the documentation

    Example:

    // declare ImageView reference
    var myImageView: ImageView
    
    // in onCreate
    myImageView = findViewById(R.id.myImageView)
    
    // change image
    myImageView.setImageResource(R.drawable.imageName)