Open In App

How to Load Image From URL in Android using Jetpack Compose?

Improve
Improve
Like Article
Like
Save
Share
Report

Most of the applications in android use images for displaying some useful information within the applications. Images are considered as easy to convey information to the user. An android application uses more than 100 different types of images. So it is not practically possible to add all the images within the application. This will increase the application size and affect the number of downloads of the application. So for handling this case most applications load the images with the help of the image URL. In this article, we will look at how we can load the image from URL in android using Jetpack Compose.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding the dependency in the dependencies section in build.gradle

Navigate to the Gradle Scripts > build.gradle and add the below dependency in the dependencies section. 

implementation("io.coil-kt:coil-compose:2.0.0-rc01")

After adding this dependency simply sync your project to install it. 

Step 3: Adding internet permissions in AndroidManifest.xml

Navigate to the app > manifest > AndroidManifest.xml and add below internet permissions in the manifest tag. 

XML




<uses-permission android:name="android.permission.INTERNET" />


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
  
import android.content.Context
import android.os.Bundle
  
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import coil.compose.rememberAsyncImagePainter
import com.example.newcanaryproject.ui.theme.*
import java.util.*
  
class MainActivity : ComponentActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying 
                // background color for our application
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    // on below line we are specifying theme as scaffold.
                    Scaffold(
                        // in scaffold we are specifying top bar.
                        topBar = {
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
                                // along with that we are specifying title for our top bar.
                                title = {
                                    // in the top bar we are specifying tile as a text
                                    Text(
                                        // on below line we are specifying 
                                        // text to display in top app bar.
                                        text = "Image From URL",
                                          
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
                                          
                                        // on below line we are 
                                        // specifying text alignment.
                                        textAlign = TextAlign.Center,
                                          
                                        // on below line we are 
                                        // specifying color for our text.
                                        color = Color.White
                                    )
                                }
                            )
                        }
                    ) {
                        // on below line we are calling image 
                        // from URL method to load image from image url.
                        imageFromURL()
                    }
                }
            }
        }
    }
}
  
// on below line we are creating an 
// image url function for our image view. 
@Composable
fun imageFromURL() {
    // on below line we are creating a column,
    Column(
        // in this column we are adding modifier 
        // to fill max size, mz height and max width
        modifier = Modifier
            .fillMaxSize()
            .fillMaxHeight()
            .fillMaxWidth()
            // on below line we are adding 
            // padding from all sides.
            .padding(10.dp),
        // on below line we are adding vertical 
        // and horizontal arrangement. 
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // on below line we are adding image for our image view. 
        Image(
            // on below line we are adding the image url 
            // from which we will  be loading our image. 
            painter = rememberAsyncImagePainter("https://media.geeksforgeeks.org/wp-content/uploads/20210101144014/gfglogo.png"),
              
            // on below line we are adding content
            // description for our image. 
            contentDescription = "gfg image",
              
            // on below line we are adding modifier for our 
            // image as wrap content for height and width. 
            modifier = Modifier
                .wrapContentSize()
                .wrapContentHeight()
                .wrapContentWidth()
        )
    }
}


Now simply run your application to see the output of it. 

Output:

Output

 



Last Updated : 10 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads