Android Kotlin: How to set ImageView image from drawable

The code defines an activity named MainActivity that displays an image in an ImageView. The image is loaded from the drawable resources of the application.

Here's a step-by-step breakdown of the code:

  1. Import statements:

    • The code starts by importing necessary libraries.
    • androidx.appcompat.app.AppCompatActivity is imported to create an activity that extends the functionality of the standard Activity class.
    • androidx.core.content.ContextCompat provides methods to access resources from the context of an activity.
    • kotlinx.android.synthetic.main.activity_main is a synthetic import that allows you to access views from the layout file activity_main.xml directly in your code.
  2. Class definition:

    • The code defines a class named MainActivity that inherits from AppCompatActivity. This class represents the main activity of the application.
  3. onCreate method:

    • The onCreate method is the entry point of the activity. It's called when the activity is first created.
    • Inside the onCreate method:
      • The setContentView(R.layout.activity_main) method sets the layout of the activity to the one defined in the activity_main.xml file.
      • The code gets a reference to the ImageView from the layout using kotlinx.android.synthetic.main.activity_main.imageView.
      • The ContextCompat.getDrawable(context, R.drawable.flower) method retrieves a drawable resource from the application's resources identified by the name flower.
      • The retrieved drawable is then set to the ImageView using the setImageDrawable method.

In summary, this code snippet demonstrates how to load an image from the drawable resources and display it in an ImageView in an Android app written in Kotlin.


MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val context = this

        // set image view image from drawable
        imageView.setImageDrawable(ContextCompat.getDrawable(
            context,R.drawable.flower
        ))
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#636C5A"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials