Overlay over an ImageView on Android

cesarferreira picture cesarferreira · Dec 27, 2013 · Viewed 23.8k times · Source

So I have a list of images that come from the web, I don't know which color are they and I want to place a text over the ImageView.

My idea is to place the ImageView, an image overlay with transparency gradient over that ImageView and the text above it. I want to mimic this behaviour:

enter image description here

Is there anyway to do this via XML?

Answer

adityajones picture adityajones · Dec 27, 2013

When you write the XML for your list items which get inflated in the getView(...) of whatever ListAdapter you've written you can surely do this.

Something like this for the list item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ImageView
    android:layout_width="320dp"
    android:layout_height="240dp"
    android:background="#ACACAC"/>

  <RelativeLayout
    android:layout_width="320dp"
    android:layout_height="240dp"
    android:background="@drawable/gradient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is your text"
        android:textColor="#000000"
        android:layout_alignParentBottom="true"/>

  </RelativeLayout>

</RelativeLayout>

Then you create that drawable/gradient. For that you can recycle the answer from here.