androidshadowandroid-elevation

android elevation hiding all view of the layout


I have layout as shown in screenshot below.

enter image description here

Code I have is as below.

<?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"
    android:background="@color/white"
    >

    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_smallxx"
        android:layout_marginTop="@dimen/margin_normalxx"
        android:layout_marginEnd="@dimen/margin_smallxx"
        android:orientation="vertical">

        .......

Now I want to add shadow for the box so I have as below.

<?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"
    android:background="@color/white"
    >

    <TextView
        style="@style/shadowStyle"
        android:elevation="20dp"
        android:layout_alignTop="@id/ll_main"
        android:layout_alignBottom="@id/ll_main"
        android:layout_alignStart="@id/ll_main"
        android:layout_alignEnd="@id/ll_main"
        />

    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_smallxx"
        android:layout_marginTop="@dimen/margin_normalxx"
        android:layout_marginEnd="@dimen/margin_smallxx"
        android:orientation="vertical">

shadowStyle is as below.

<style name="shadowStyle" parent="@android:style/TextAppearance">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:text"></item>
    <item name="android:background">@drawable/myrect</item>
    <item name="android:outlineSpotShadowColor">@color/shadow_color</item>
</style>

And myrect.xml shape as below.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <corners android:radius="20dp" />
</shape>

When I add this all below view gets hidden which is weird.

App screenshots are as below.

enter image description here

I give shadow_color as #FF00FF for testing purpose only.

Any idea how to make it proper? Am I missing something...


Solution

  • Below is what I did as per mãĴď comments

    <?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:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/white">
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_smallxx"
            android:layout_marginTop="@dimen/margin_normalxx"
            android:layout_marginEnd="@dimen/margin_smallxx"
            app:cardCornerRadius="10dp"
            app:cardElevation="20dp"
            android:outlineSpotShadowColor="@color/shadow_color"
            >
    
            <LinearLayout
                android:id="@+id/ll_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                ........
            </LinearLayout>
        </CardView>
    </RelativeLayout>
    

    This means we will have card view and put whole layout inside it...