Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set foreground attribute to other non FrameLayout view

I would like to know how to apply or emulate foreground effect in a view different from FrameLayout, as LinearLayout or RelativeLayout

This is what I have now:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    android:foreground="@drawable/foreground_row">

    ...

</FrameLayout>

And I want something like:

<?xml version="1.0" encoding="utf-8"?>
    <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"
        app:foreground="@drawable/foreground_row">

        ...

    </RelativeLayout>

Thanks in advance!!

like image 924
cesards Avatar asked May 28 '13 08:05

cesards


People also ask

What is foreground attribute in android?

The documentation states the android:foreground defines the drawable to draw over the content , it doesn't state anywhere that the content has to be a ViewGroup . And the tag is defined for the View class.

What is foreground gravity in android?

3. android:foregroundGravity. Defines the gravity to apply to the foreground drawable. The gravity defaults to fill. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.

What is foreground color android?

Foreground colors(colors which are before background colours) are the colours you see on the screen before the background colours.

What is the use of FrameLayout in android?

Android Framelayout is a ViewGroup subclass that is used to specify the position of multiple views placed on top of each other to represent a single view screen. Generally, we can say FrameLayout simply blocks a particular area on the screen to display a single view.


1 Answers

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

like image 113
Antonio Avatar answered Oct 09 '22 15:10

Antonio