Android ImageView Using Kotlin With Example

In this article, we will learn about android ImageView using Kotlin. We will go through various example that demonstrates how to use different attributes of ImageView. For example,

In this article, we will get answer to questions like –

  • What is ImageView?
  • Why should we consider ImageView while designing ui for any app?
  • What are possibilities using ImageView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin ImageView Output Android ImageView Using Kotlin

Tutorialwing Kotlin ImageView Output

Getting Started

We can define android ImageView widget as below –

ImageView is subclass of view that displays image. It is also used to handle image tinting and scaling.

Now, how do we use ImageView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ImageView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ImageView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ImageView in Kotlin

Follow steps below to use ImageView in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ImageView</string>
        <string name="this_is_imageview">This is imageView</string>
        <string name="change_image">Change Image</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btnChangeImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:text="@string/change_image"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/this_is_imageview"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnChangeImage" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.imageview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.imageview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupImageView()
    	}
    
    	private fun setupImageView() {
    		val imgResId = R.drawable.ic_launcher_background
    		var resId = imgResId
    		binding.imageView.setImageResource(imgResId)
    
    		binding.btnChangeImage.setOnClickListener {
    			resId =
    				if (resId == R.drawable.ic_launcher_background)
    					R.mipmap.ic_launcher
    				else
    					R.drawable.ic_launcher_background
    			binding.imageView.setImageResource(resId)
    		}
    	}
    }
    

    Here, we have accessed imageView using kotlin file (i.e. MainActivity.kt file) in the application. Then, we have set an image resource in it. After that, we have set a click listener on button that will change the button in the imageView whenever it is clicked. Similarly, you can perform any operation in imageView.

Now, run the application. We will get output as below –

Tutorialwing Kotlin ImageView Output Android ImageView Using Kotlin

Tutorialwing Kotlin ImageView Output

Different Attributes of ImageView in XML

Now, we will see how to use different attributes of Android ImageView using Kotlin to customise it –

Set Id of ImageView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ImageView using android:id attribute like below –

    <ImageView
        android:id="@+id/imageView_ID"
        />

Here, we have set id of ImageView as imageView_ID using android:id=”” attribute. So, if we need to reference this ImageView, we need to use this id – imageView_ID.
Learn to Set ID of ImageView Dynamically

Set Width of ImageView

We use android:layout_width=”” attribute to set width of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ImageView Dynamically

Set Height of ImageView

We use android:layout_height=”” attribute to set height of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ImageView Dynamically

Set Padding of ImageView

We use android:padding=”” attribute to set padding of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ImageView using android:padding=”” attribute.
Learn to Set Padding of ImageView Dynamically

Set Margin of ImageView

We use android:layout_margin=”” attribute to set margin of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ImageView using android:layout_margin=”” attribute.
Learn to Set Margin of ImageView Dynamically

Set Background of ImageView

We use android:background=”” attribute to set background of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ImageView using android:background=”” attribute.
Learn to Set Background of ImageView Dynamically

Set Visibility of ImageView

We use android:visibility=”” attribute to set visibility of ImageView.
We can do it as below –

    <ImageView
        android:id="@+id/imageView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ImageView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ImageView Dynamically

Till now, we have see how to use android ImageView using Kotlin. We have also gone through different attributes of ImageView to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android ImageView Widget

Below are the various attributes that are used to customise android ImageView Widget. However, you can check the complete list of attributes of ImageView in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of imageView are –

Sr. XML Attributes Description
1 android:adjustViewBounds Use this attribute to auto adjust imageView’s boundaries to maintain the aspect ratio of it’s drawable
2 android:baseline It is used to set the offset of the baseline within this view.
3 android:baselineAlignBottom It is used to set the baseline aligned with it’s bottom edge.
4 android:cropToPadding It is used to crop the image to fit within it’s padding
5 android:maxHeight It is used maximum height of the view.
6 android:maxWidth It is used maximum width of the view.
7 android:scaleType It is used to define the way image will be resized or moved to match the size of the imageView
8 android:src It is used to set a drawable of the imageView
9 android:tint It is used to tint the color of the image.
10 android:tintMode It is used to set blending mode used to apply the image tint

Some of the popular attributes of ImageButton inherited from View are –

Sr. XML Attributes Description
1 android:alpha It is used to set alpha of the view
2 android:autofillHints It is used to set the data to be shown to auto fill in the view
3 android:background It is used to set background of the view
4 android:backgroundTint It is used to set tint to apply to the background
5 android:backgroundTintMode It is used to set blending mode used to apply the background tint
6 android:clickable It is used to set whether this view is clickable or not
7 android:elevation It is used to set elevation to the view
8 android:fitsSystemWindows It is used to adjust view layout based on system windows
9 android:focusable Controls whether this view can take focus
10 android:id It is used to set unique id to the view
11 android:onClick It is used to define what to do when this view is clicked
12 android:padding It is used to set padding to the view
13 android:tag It is used to set tag to the view
14 android:tooltipText It is used to set tooltip text to be shown when hovered on the view
15 android:visibility It is used to set visibility of the view

We have seen different attributes of ImageView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ImageView, how can we use android ImageView using Kotlin ? etc. We also went through different attributes of android ImageView.

Leave a Reply