Search code examples
androidxmlandroid-layoutandroid-constraintlayout

I have created a layout for recycleView, when I am adding the image inside constraint layout, there is always a default margin in the top


I have created a layout for recycleView, when I am adding the image inside constraint layout, there is always a default margin in the top

Here is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginBottom="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:background="@drawable/rv_bg">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/services"
        android:scaleType="centerCrop"/>


</androidx.constraintlayout.widget.ConstraintLayout>

Here I am attaching the screenshot of the layout, If anyone having the solution, please share

Screenshot


Solution

  • I got the solution to this problem, if you are too facing the problem, instead of ImageView, you can use this library called RoundedImageView

    Github Link: https://github.com/vinc3m1/RoundedImageView

    1. Add the dependency to your app-level Gradle module implementation 'com.makeramen:roundedimageview:2.3.0'

    2. Sample XML file


    <com.makeramen.roundedimageview.RoundedImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/image"
            android:src="@drawable/services"
            android:scaleType="centerCrop"
            app:riv_corner_radius_top_left="10dp"
            app:riv_corner_radius_top_right="10dp"
            app:riv_mutate_background="false"
            app:riv_oval="false"
            android:layout_height="200dp"
            android:layout_width="match_parent"
            tools:ignore="MissingConstraints" />
    

    This library helped me to solve my problem, do check out this once if you are also facing the same kind of issues.