How to Load Image From URL in Android using Jetpack Compose?
Last Updated :
10 Jun, 2022
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:
Similar Reads
How to Play Audio From URL in Android using Jetpack Compose? Many apps require the feature to add the audio feature in their application and there are so many audio files that we have to play inside our application. If we will store so many audio files inside our application, then it will increase the size of the app and this may reduce the user base due to t
7 min read
How to Display Image From Image File Path in Android using Jetpack Compose? ImageViews are used to display images in different formats within the Android Application. We can display images within our Image View from the image file name, bitmap, drawable file, and the image URL as well. In this article, we will take a look at How to load images from user devices within Image
5 min read
ImageView in Android using Jetpack Compose Images, graphics, and vectors attract many users as they provide a lot of information in a very informative way. ImageView in Android is used to display different types of images, from drawables to Bitmaps. In this article, we will take a look at the implementation of an ImageView in Android using J
3 min read
Image Color Picker in Android using Jetpack Compose Many android applications require functionality in which the user should be able to pick the color from the image which is present within the android application. In this article, we will take a look at How to implement Image Color Picker in Android applications using Jetpack Compose. We will be abl
5 min read
Generate PDF File in Android using Jetpack Compose There are many apps in which data from the app is provided to users in the downloadable PDF file format. So in this case we have to create a PDF file from the data present inside our app and represent that data properly inside our app. So by using this technique we can easily create a new PDF accord
4 min read
How to Share Image From URL with Intent in Android? In this article, we will see how can we share images and text with Android Intent. In this activity URL of an image to be shared will be given with extra text and we will open the Dialog where the user chooses using which he wants to share this image as shown on the screen. A sample video is given b
5 min read
How to Load Any Image From URL Without Using Any Dependency in Android? Many applications display images from the internet using third-party APIs like Glide and Picasso to load images. This means that such applications partly depend on these services to keep themselves working fine. To make the application better, one should write their own code rather than depending on
3 min read
Deep Links in Android using Jetpack Compose A deep link is a URL that is used to direct users to a specific page or specific activity within the application. We can also pass data to our application with the help of these deep links. In this article, we will take a look at How to implement Deep Links in Android using Jetpack Compose. Step by
7 min read
How to Load SVG from URL in Android ImageView? It is seen that many Android apps require to use of high-quality images that will not get blur while zooming. So we have to use high-quality images. But if we are using PNG images then they will get blur after zooming because PNG images are made up of pixels and they will reduce their quality after
4 min read