How to set foreground attribute to other non FrameLayout view

The idea is to surround your layout with a FrameLayout, and set the selector and the onClick event to this layout.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/selectableItem"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:foreground="@drawable/foreground_row"
    >

   <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_background">

        ...

    </RelativeLayout>
</FrameLayout>

You can find a full explanation at my blog:

http://antonioleiva.com/unveiling-bandhook-foreground-any-layout/

Or you can extend rhis FRelativeLayout https://gist.github.com/shakalaca/6199283

Leave a Comment