Open In App

Circular ImageView in Android using Jetpack Compose

Last Updated : 13 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Circular ImageView is used in many of the apps. These types of images are generally used to represent the profile picture of the user and many more images. We have seen the implementation of ImageView in Android using Jetpack Compose. In this article, we will take a look at the implementation of Circle ImageView in Android using Jetpack Compose.  

Step by Step Implementation

Step 1: Create a New Project

To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.

Step 2: Add an Image to the drawable folder

After creating a new project we have to add an image inside our drawable folder for displaying that image inside our ImageView. Copy your image from your folder’s location and go inside our project. Inside our project Navigate to the app > res > drawable > Right-click on the drawable folder and paste your image there.  

Step 3: Working with the MainActivity.kt file

After adding this image navigates to the app > java > MainActivity.kt and add the below code to it. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.graphics.drawable.shapes.Shape
import android.media.Image
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.InteractionState
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GFGAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    // at below line we are calling 
                      // our function for button.
                    CircleImg();
                }
            }
        }
    }
}
  
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    GFGAppTheme {
        CircleImg();
    }
}
  
@Composable
fun CircleImg() {
  
    Column(
          
          // we are using column to align our imageview 
          // to center of the screen.
        modifier = Modifier.fillMaxWidth().fillMaxHeight(),
        
        // below line is used for specifying 
          // vertical arrangement.
        verticalArrangement = Arrangement.Center,
        
        // below line is used for specifying 
          // horizontal arrangement.
        horizontalAlignment = Alignment.CenterHorizontally,
  
        ) {
        // creating a card for creating a circle image view. 
        Card(
            // below line is use to add size to our image view and 
              // test tag is use to add tag to our image. 
            modifier = Modifier.preferredSize(100.dp).testTag(tag = "circle"),
             
              // below line is use to 
              // add shape to our image view. 
            shape = CircleShape,
            
            // below line is use to add 
              // elevation to our image view. 
            elevation = 12.dp
        ) {
            // below line we are creating a new image.
            Image(
                // in below line we are providing image
                  // resource from drawable folder. 
                imageResource(id = R.drawable.gfgimage),
                  
                  // below line is use to give scaling
                  // to our image view.
                contentScale = ContentScale.Crop,
                  
                  // below line is use to add modifier
                  // to our image view. 
                modifier = Modifier.fillMaxSize()
            )
        }
    }
}


Now run your app and see the output of the app. 

Output:

Circluar ImageView in Android using Jetpack Compose Output

 



Similar Reads

ImageView in Android using Jetpack Compose
Images, Graphics, and vectors attract so many users as they tell so much information in a very informative way. ImageView in Android is used to display different types of images from drawable or in the form of Bitmaps. In this article, we will take a look at the implementation of an ImageView in Android using Jetpack Compose. Attributes of ImageVie
3 min read
Android Jetpack Compose - Display Mirror of an ImageView
Many times in the android application we have to display a mirror image of an image view within our android application to show its mirror image. In this article, we will take a look at How to add a mirror image effect to our image view in android using Jetpack Compose. Step by Step Implementation Step 1: Create a New Project in Android Studio To c
5 min read
Android Jetpack Compose - Interoperability Using Compose in XML Layouts
Writing UI in jetpack compose can be fun, but can be difficult to migrate your whole project into Compose at one time. Fortunately, Jetpack Compose provides Interoperability api to use compose in existing XML views so that you can migrate to compose slowly. Prerequisites: Knowledge of Jetpack Compose.Knowledge of KotlinKnowledge of Android’s View.
3 min read
Create a Circular Button with an Icon in Android Jetpack Compose
In Android, Buttons are the most basic and frequently used UI element that lets users call a function or start a task from the application context. It is provided to the user to give input to the application with the help of a click. These Buttons are largely customizable and visual attributes can be altered according to the theme of the applicatio
3 min read
How to Create a Circular ImageView in Android Without using Any Library?
This article aims to help in How to create a Circular image view in Android without using any library in an Android Application. A Simple Circular ImageView can be made with a White Border and Transparent Content of any shape without using any Library. The code has been given in both Java and Kotlin Programming Language for Android. Step by Step Im
2 min read
How to Create Circular ImageView in Android using CardView?
Displaying an Image in Android is done easily using ImageView. But what if one wants to display a circular image? It is seen that many Android apps use CircularImageView to show the profile images, status, stories, and many other things but doing this with a normal ImageView is a bit difficult. This article will help to create a circular image usin
3 min read
Android Runtime Permissions with Dexter using Android Jetpack Compose
Android applications require the usage of hardware devices within the android applications such as microphones or cameras. For using these devices within any android application. Permissions for using this hardware have to be provided to the application. For giving these permissions to the application runtime permissions are used. Handling permissi
7 min read
Start a New Activity using Intent in Android using Jetpack Compose
In Android OS, an Intent is a mechanism used to navigate a user to another activity or application to achieve a specific task. In general, Intents are used to progress to the next activity or come back to the previous activity. [caption width="800"] [/caption] In this article, we will show you how you could start a New Activity using Intent in Andr
4 min read
How to Update Data in API using Retrofit in Android using Jetpack Compose?
Android applications use APIs within Android Applications to access data from databases. We can perform several operations using APIs such as Reading, Writing, and Updating our Data in the Database. In this article, we will take a look at How to Update Data in API using Retrofit in Android using Jetpack Compose. Note: If you are seeking Java code f
9 min read
How to Post Data to API using Volley in Android using Jetpack Compose?
APIs are used within Android Applications to interact with a database to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post data to API in android using Volley in Jetpack Compose. Note: If you are
9 min read