Search code examples
androidandroid-layoutlayer-list

Cannot display layer-list in image view


I want to add a layer-list as a background to an imageview widget in android.

I have tried adding it as android:background="@drawable/layer_list_xmlfile
and also as android:src="@drawable/layer_list_xmlfile.

The preview window shows nothing when I add the layer-list and neither does it show up on building the apk(used on genymotion as well as on real Android device).

Here's what I get:

no idea what error icon means

The layer-list should be displayed at the position where there's a red dot on the preview window.

My imageview layout code:

<ImageView android:id="@+id/service_offers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/customoffertag"
            android:scaleType="center"
            android:contentDescription="@string/service_offer"/>

My layer-list(customoffertag.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/custom_layer_item">
        <rotate
            android:fromDegrees="-65"
            android:toDegrees="45"
            android:pivotX="-55%"
            android:pivotY="1%" >
            <shape
                android:shape="rectangle" >
                <stroke android:color="@android:color/transparent"
                    android:width="10dp"/>
                <solid
                    android:color="#008475" />
            </shape>
        </rotate>
    </item>
</layer-list>

Please can anyone tell me what that error icon is indicating, or where am I going wrong.

Full layout code:

<?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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="2dp"
    android:padding="5dp" >

<android.support.v7.widget.CardView
    android:id="@+id/card_list_service"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    android:padding="5dp"
    android:background="#FFFFFF"
    android:elevation="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView android:id="@+id/service_pic"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginRight="3dp"
            android:layout_marginEnd="3dp"
            android:layout_centerVertical="true"
            android:scaleType="fitXY"
            android:contentDescription="@string/item_pic"/>

        <TextView android:id="@+id/service_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/service_pic"
            android:layout_toRightOf="@id/service_pic"
            android:layout_alignTop="@id/service_pic"
            android:padding="5dp"
            android:gravity="center"
            android:text="@string/service_provider_name"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="normal"
            android:typeface="monospace"
            android:layout_marginBottom="2dp"/>

        <ImageView android:id="@+id/pickup_service"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/service_text"
            android:layout_toRightOf="@id/service_text"
            android:layout_alignBottom="@id/service_text"
            android:layout_alignParentTop="true"
            android:src="@drawable/pickup"
            android:scaleType="centerCrop"
            android:padding="5dp" />

        <ImageView android:id="@+id/service_offers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/customoffertag"
            android:scaleType="center"
            android:contentDescription="@string/service_offer"/>

        <TextView android:id="@+id/service_contact_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/service_text"
            android:layout_toRightOf="@id/service_pic"
            android:layout_toEndOf="@id/service_pic"
            android:layout_alignStart="@id/service_text"
            android:layout_alignLeft="@id/service_text"
            android:padding="5dp"
            android:gravity="center"
            android:text="@string/service_provider_contact"
            android:textColor="#848484"
            android:textStyle="normal"
            android:textSize="16sp"
            android:typeface="monospace" />

        <TextView android:id="@+id/service_dist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/service_contact_no"
            android:layout_toEndOf="@id/service_pic"
            android:layout_toRightOf="@id/service_pic"
            android:layout_alignLeft="@id/service_contact_no"
            android:layout_alignStart="@id/service_contact_no"
            android:padding="5dp"
            android:gravity="center"
            android:text="@string/service_provider_dist"
            android:textColor="#848484"
            android:textSize="15sp"
            android:textStyle="normal"
            android:typeface="monospace"/>

        <Button android:id="@+id/booknow_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/service_contact_no"
            android:layout_toRightOf="@id/service_dist"
            android:layout_toEndOf="@id/service_dist"
            android:layout_alignBottom="@id/service_dist"
            android:padding="5dp"
            android:text="@string/booknow"
            android:textSize="17sp"
            android:textStyle="normal"
            android:textColor="#FFFFFF"
            android:typeface="monospace"
            android:gravity="center"
            android:background="#008475"
            android:elevation="2dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/review_2"
            android:textColor="#848484"
            android:textSize="17sp"
            android:id="@+id/review_text"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:padding="3dp"
            android:layout_marginEnd="3dp"
            android:layout_marginRight="3dp"
            android:gravity="end" />

        <TextView android:id="@+id/review_text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/review_1"
            android:textSize="17sp"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/review_text"
            android:layout_toStartOf="@id/review_text"
            android:layout_alignTop="@id/review_text"
            android:layout_marginRight="0dp"
            android:layout_marginEnd="0dp"
            android:padding="3dp"
            android:background="#424242"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>


Solution

  • Layer-list gets displayed in the imageview widget only when the height and width are not set to wrap_content but to a specific dimension.

    For me the dimensions were height=100dp; width=50dp

    Tried the same thing with a simple color

    android:background="#000000"  
    

    and also with android:src="#000000"

    Both won't show anything until width and height of imageview is defined to be a specific dp value.