1

I've been having a lot of trouble with this layout.

Basically I have 2 linears, one that is slightly grey with that shadow on bottom and below another linear just white. My trouble here is adding that shadow to the end of the first linear. I tried "elevation" but that is now what i need since i want the shadow to be inside the linear kinda if the last 10dp are from a different color but I'm not getting it right.

enter image description here

This is my layout so far, i thought of putting a view inside the first linear but its not working that good.

Any ideas how to do this?

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

    <LinearLayout
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:background="#f8f8f8"
        android:layout_height="0dp">

        <android.support.v7.widget.CardView
            app:cardCornerRadius="10dp"
            app:cardElevation="10dp"
            android:layout_width="312dp"
            android:layout_height="204dp"
            app:cardBackgroundColor="@android:color/holo_red_light"/> 
    </LinearLayout>


    <LinearLayout
        android:background="#ffffff"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </LinearLayout>
</LinearLayout>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hugo
  • 102
  • 1
  • 9
  • check [link1](https://stackoverflow.com/questions/21211870/android-view-shadow), [link2](https://stackoverflow.com/questions/24095223/android-linearlayout-add-border-with-shadow-around-a-linearlayout) – Mohammed Alaa Jan 02 '20 at 23:09

2 Answers2

3

Try using below line in the linearlayout under which your cardview is sitting.

android:elevation="8dp"

Let the cardElevation part in your cardView be as it is.

card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false"

Also just a suggestion :

If you're using android:layout_weight in both child linearLayout put android:weightSum="2" in your parent linearLayout as mentioned in answer given by @Ming Leung here

For more information on android:weightSum="2" you can refer to answers on this page

This page would help you for elevation in cardview

Hope that helps.

Omkar C.
  • 755
  • 8
  • 21
  • i probably lead to some confusion, so i will edit the image, my cardview is fine the problem is the shadow in the linear where the cardview is. – Hugo Jan 03 '20 at 01:22
  • Hi @Hugo check out my updated answer, let me know if you've any other queries. – Omkar C. Jan 04 '20 at 01:34
1

I was able to get it to work using a ConstraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="312dp"
        android:layout_height="204dp"
        app:cardBackgroundColor="@android:color/holo_red_light"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Cory Roy
  • 5,379
  • 2
  • 28
  • 47