How to Create a Circular ImageView in Android using CardView?


A Circular image view in Android alludes to a picture view component that shows a picture in a circular shape. It is accomplished by applying a circular veil to the ImageView and trimming the picture to fit inside the circular boundary. This makes an outwardly engaging impact and is commonly utilised for showing profile pictures or circular symbols. By utilising the CardView holder, designers can effectively make a Circular image view by setting the image view's corner sweep to half of its width or height, successfully changing it into a circular shape. This approach gives a straightforward and rich arrangement for accomplishing circular picture shows in Android applications.

Methods Used

  • Manual implementation

Manual Implementation

To make a Circular image view in Android utilising CardView, you'll manually actualize it by customising the appearance and behaviour of the image view through code. Manual execution alludes to the method of characterising and controlling the ImageView's properties programmatically instead of depending exclusively on predefined components or libraries. It includes making a circular shape for the ImageView, setting the vital qualities such as measure and foundation colour, and applying it to a CardView for included styling and rise. This approach permits engineers to have exact control over the ImageView's circular shape and other visual viewpoints, resulting in a customised and outwardly engaging client interface.

Algorithm

  • Make a format record (XML) that incorporates a CardView and an ImageView inside it. Set the essential qualities for the CardView, such as foundation colour and elevation.

  • In your Java lesson, get a reference to the ImageView utilising the findViewById() strategy and dole it out to a variable.

  • Set the shape of the ImageView to circular by making a custom shape drawable resource record with a circular shape. You'll be able to characterise the shape in XML using the tag and set the shape sort to "oval" or "circle".

  • Apply the circular shape to the ImageView utilising the setImageDrawable() or setBackground() strategies, depending on the required result. Pass the custom shape as the argument.

  • Optionally, you'll be able to customise the appearance of the CardView to improve the circular impact. For example, you'll adjust the corner sweep or add cushioning to the CardView.

  • Update the ImageView with the specified picture utilising the setImageResource() or setImageURI() strategy, depending on the picture source. Pass the resource ID or URI of the picture as the argument.

  • Handle any extra rationale or usefulness related to the Circular ImageView according to your application's necessities. For illustration, you'll include press audience members or perform activities when the ImageView is connecting.

  • Build and run the application to see the Circular ImageView in activity. The ImageView ought to be shown as a circular shape inside the CardView.

Program

Add Dependencies

Open the build.gradle file for the Module and include the line 'interface 'androidx.cardview:cardview:1.0.0'' to the list of conditions. Spare the report; at that point, synchronise the extension by clicking the "Match up Presently" button. This will ensure that the desired library is imported and utilised. Additionally, locate the code segment within the activity_main.xml record, open it, and alter 'android:src="@drawable/your_image"' to the name of the suitable picture asset. The new picture within the assigned area will show up within the overhauled UI as a result.

implementation 'androidx.cardview:cardview:1.0.0'

XML Program

<?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"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/white"
   tools:context=".MainActivity">

   <androidx.cardview.widget.CardView
       android:id="@+id/cardView"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_marginTop="150dp"
       android:layout_marginBottom="16dp"
       android:layout_marginStart="16dp"
       android:layout_marginEnd="16dp"
       app:cardCornerRadius="100dp"
       app:layout_constraintBottom_toTopOf="@+id/button"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent">

       <ImageView
           android:id="@+id/imageView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:scaleType="centerCrop"
           android:src="@drawable/my_image"
           android:contentDescription="@string/image_description" />
   </androidx.cardview.widget.CardView>

   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="16dp"
       android:text="@string/click_me"
       android:textSize="16sp"
       app:layout_constraintBottom_toTopOf="@+id/textView"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent" />

   <TextView
       android:id="@+id/textView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="16dp"
       android:gravity="center"
       android:text="@string/circular_image_view"
       android:textColor="@android:color/black"
       android:textSize="20sp"
       android:textStyle="bold"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Main Java Program

private ImageView circularImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_circular_image_view);

   circularImageView = findViewById(R.id.circularImageView);

   // Set click listener for the circularImageView
   circularImageView.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           showToast("This is a Circular ImageView");
       }
   });
}

/**
 * Display a Toast message.
 *
 * @rati message The message to be displayed.
 */
private void showToast(String message) {
   Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

Output

Conclusion

This article provides a step-by-step tutorial on how to use CardView in Android to display a circular image. It clarifies the idea behind a circular image and the visual appeal it offers when profile photographs or circular images are displayed. The manual execution strategy is highlighted, enabling engineers to correctly control the circular form of the ImageView and other visual viewpoints. The article combines a formula and lines of code that demonstrate how to change an ImageView's settings, apply a circular shape, and respond to press events. By and large, it provides in-depth explanation and practical application for completing circular picture appearances in Android applications.

Updated on: 31-Jul-2023

667 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements