Shadow and Text on ImageView in Android

In this article, we will learn how to create a shadow on ImageView in Android. This article is the part of the Today I Learnt category.

Here is the video tutorial

So let’s started.

Let me tell you about its usage. Basically it is mostly used in e-commerce Applications.

Now, let’s understand the structure of this. Basically, it is working like a stack where every view stacked over one another.

Create a drawable file for shadow view and assign name as image_shadow and add below code in this file.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <corners
            android:radius="10dp"
            />
        <gradient
            android:angle="270"
            android:centerX="300%"
            android:startColor="#00000000"
            android:endColor="#99000000"
            android:type="linear"
            />
        <size
            android:width="270dp"
            android:height="60dp"
            />
        <stroke
            android:width="1dp"
            android:color="#878787"
            />
    </shape>

Now, goto xml file and add the below code into it and set this drawable file as view’s background.

    <androidx.appcompat.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:scaleType="centerCrop"
                android:src="@drawable/shopping_image"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
    
            <View android:id="@+id/view"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:background="@drawable/image_shadow"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
    
            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textColor="@android:color/white"
                android:text="Write your text here"
                android:textSize="25sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="@+id/view"/>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.appcompat.widget.LinearLayoutCompat>

Note: Follow this code line by line.

In this code, I had set components’ constraints like a stack where every component stacked with each other.

Cheeeerrrsss 🍷 , That’s all for now.

Thanks for reading if you want any help then connect with on my Blog, Github, Twitter.

See also