55

I want to add a gradient on the bottom of my image . Something like this :

enter image description here

I tried something like this but I only get the gradient no image..

    <ImageView
    android:id="@+id/trendingImageView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/trend_donald_sterling"
    android:src="@drawable/trending_gradient_shape"
  />

trending_gradient_shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="@android:color/darker_gray"
        android:startColor="@android:color/darker_gray" />

    <corners android:radius="0dp" />

</shape>
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
user1163234
  • 2,407
  • 6
  • 35
  • 63

7 Answers7

72

You need two layers: An ImageView, and a View on top of that with your gradient as android:background. Put these two Views in a FrameLayout:

<FrameLayout
    ... >

    <ImageView
        ...
        android:src="@drawable/trend_donald_sterling" />

    <View
        ...
        android:background="@drawable/trending_gradient_shape"/>


</FrameLayout>
nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • 2
    Don't forget to add an alpha value to your colors. Try starting with `android:endColor="#00000000" android:startColor="ff000000"`, and play around with it to suit your needs. – nhaarman Jun 02 '14 at 09:59
  • 2
    Too much work to do for the UI. Personally, I prefer the solution with the single ImageView. I have not done the maths but seems more effective in performance – Vaios Mar 03 '19 at 22:01
49

Simply set the alpha value in your gardient.xml:

Your imageView:

android:background="@drawable/trend_donald_sterling"
android:src="@drawable/trending_gradient_shape"

Your gradient xml file:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="90"
    android:endColor="#00ffffff"
    android:startColor="#aa000000"
    android:centerColor="#00ffffff" />

<corners android:radius="0dp" />
</shape>

In the color value, the first two places after # correspond to the alpha value, while the rest are the actual color value in R G B format, two for each.

nmilcoff
  • 1,084
  • 5
  • 20
Kaustuv
  • 811
  • 5
  • 9
31

try using the "foreground" attribute in your imageview

<ImageView
        ...
        android:src="@drawable/trend_donald_sterling"
        android:foreground="@drawable/trending_gradient_shape" />

it worked for me.

swetabh suman
  • 1,949
  • 2
  • 19
  • 24
  • This worked for my purposes, but if you only want the gradient to go up to a certain height having a view on top of the ImageView makes more sense. – sotrh Apr 10 '17 at 18:21
  • 5
    Note: Attribute `android:foreground` has no effect on API levels lower than 23 (current min is 16). This is what IDE shows me. – Vadim Kotov Dec 25 '18 at 10:30
12

Use android:foreground="..." instead of android:background="..."

Now you won't need to put ImageView and View inside a FrameLayout!

So your final code will be:

ImageView

<ImageView
    ...
    android:foreground="@drawable/trend_donald_sterling"/>

Drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#00ffffff"
        android:startColor="#aa000000"
        android:centerColor="#00ffffff" />

    <corners android:radius="0dp" />
</shape>
Slick Slime
  • 649
  • 7
  • 19
  • Note: Attribute android:foreground has no effect on API levels lower than 23 (current min is 16). This is what IDE shows me. – Vadim Kotov Dec 25 '18 at 10:31
  • As you will see in my Answer I am using `android:background` not `android:foreground`. For your question about API level warning please refer to another SO question. – Slick Slime Dec 26 '18 at 09:21
  • But you've written: `Use android:foreground="..." instead of android:background="..."` for some reason. – Vadim Kotov Dec 26 '18 at 09:24
  • Oops that's my bad. In that case you either change your `minSdkVersion` to above 22 or follow other questions that requires `FrameLayout` – Slick Slime Dec 26 '18 at 09:41
5

this is how im gonna do, i used relative layout as my parent layout, use the following code

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/img_sample"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/gradiant"/>
        <LinearLayout
            android:layout_marginLeft="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1">
            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.55"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:text="Events"
                android:gravity="bottom"
                android:textStyle="bold"
                android:textSize="18sp"
                android:textColor="#ffffff"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:text="Some description about the events goes here"
                android:textSize="14sp"
                android:textColor="#ffffff"/>
        </LinearLayout>
    </RelativeLayout>

hope you can figure out, here i attach my gradiant code below.use it inside the drawable folder....

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="90"
    android:endColor="#00ffffff"
    android:startColor="#aa000000"
    android:centerColor="#00ffffff" />

<corners android:radius="0dp" />
</shape>
Ashana.Jackol
  • 3,064
  • 28
  • 22
2

This is an easy way that creates a similar effect yet doesn't actually have the image disappear. Sometimes using the foreground attribute is not the best for the gradient, especially if using a motionlayout or you have nested scrollviews. Create an entirely new imageview and set the background to the gradient.

XML With Both Imageviews

<ImageView
        android:id="@+id/main_imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:src="@drawable/peakpx__1_"
        ads:layout_constraintHeight_percent=".55"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/main_imageView_gradient"
        android:layout_width="0dp"
        android:layout_height="0dp"
        ads:layout_constraintHeight_percent=".55"
        android:background="@drawable/gradient_theme_background"
        app:layout_constraintEnd_toEndOf="@id/main_imageView"
        app:layout_constraintBottom_toBottomOf="@id/main_imageView"
        app:layout_constraintStart_toStartOf="@id/main_imageView" />

Then for the gradient, I use black #000000 for darker themes, and white #ffffff for lighter ones. A lot of answers I see on this are not adding the center color. This is important if you want to have the gradient start closer to the edge of the image.

gradient_background

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:type="linear"
    android:endColor="#ff000000"
    android:centerColor="#00000000"
    android:startColor="#00000000"/>
</shape>
1
**1> Create file black_shadow.xml**

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:angle="270"
                android:startColor="#00000000"
                android:centerColor="#9c000000"
                android:endColor="#000000"
                android:type="linear" />
        </shape>
    </item>
</selector>
    
**2> Just add below line in Imageview.**

  android:foreground="@drawable/black_shadow"

For android lower than API 23 foreground is not available, you can use a View constrained to be over the ImageView and set the background's View to this drawable.

Jesús Barrera
  • 400
  • 1
  • 5
  • 15
MEGHA DOBARIYA
  • 1,622
  • 9
  • 7