1

I can not draw the shadow with setElevation.I do not want to use a drawing to do this. Here is the code:
This is the parent.
Page.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/background">

    <LinearLayout
        android:id="@+id/pp_cont_new_list"
        android:background="@color/background"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="5dp">
    </LinearLayout>

</LinearLayout>

Here I build a simple layout.
PersonalPage.Java

final LinearLayout ROOT = (LinearLayout) findViewById( R.Id.pp_cont_new_list);
final LinearLayout LL = new LinearLayout(context); //Container
LL.setLayoutParams (new LinearLayout.LayoutParams(
    Generic.PARAM_MP, //eq. match_parent
    (int) context.getResources ().getDimension (R.dimen.bt_preview_list_height)
));
LL.setBackgroundResource (R.drawable.background_new_list);
LL.setElevation (5f);
LL.setClickable (true);

ROOT.addView( LL );
/*
 * other code inner the container
 */

this background
background_new_list.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp"/>
            <solid android:color="@color/background"/>
        </shape>
    </item>
    <item>
        <ripple android:color="@color/textColorAlpha"/>
    </item>
</layer-list>

I would like an explanation of my error.
And I would also like to know what to use for pre-LOLLIPOP.

Thorny84
  • 198
  • 11
  • Solution for lollipop and above. The right way to get a child view to show shadow is to set padding on the parent and set `android:clipToPadding="false"` on that parent. Here is the explanation. https://stackoverflow.com/a/27518160/886148 – wick.ed Jun 19 '17 at 20:03
  • for wick.ed: As you see the padding there, but even if I add your solution does not work – Thorny84 Jun 19 '17 at 20:06
  • did you add padding to the parent layout? Could you show me the updated code ? Also, the value for `color/background` – wick.ed Jun 19 '17 at 21:36

3 Answers3

1

set elevation just doesnot work for pre-lolipop

You can create a custom button image that has a shadow and set it as button background..or any view's background to give shadow effect

Shubham
  • 41
  • 3
0

use ViewCompat.setElevation(your_view(in your case LL), elevation_amount); but for devices pre-Lollipop this will not have effect, for pre-Lollipop cases you might use:

android:background="@android:drawable/dialog_holo_light_frame"
Meikiem
  • 1,546
  • 2
  • 9
  • 18
0

This is working if you don't mind doing things in xml. I tried it out. I did not have background color value that you are using so I used a different color for parent layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8"
android:paddingBottom="20dp"
android:clipToPadding="false"
android:translationZ="6dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@drawable/background_new_list"
    android:clipToPadding="false"
    android:elevation="25dp"
    android:orientation="vertical"
     />

Note: I am using a higher elevation value just to make it easier to check if it's working. you can adjust it to what you need.

wick.ed
  • 403
  • 7
  • 14