"android:elevation=" doesn't work on devices pre-Lollipop with compile API21
Asked Answered
S

4

40

I'm trying to use "android: elevation =" in my application but once I run it does not appear in the device with android 4.1.2

gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.alvaro.proyectocaronte"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

layout.xml

<RelativeLayout
                android:layout_width="1100dp"
                android:layout_height="fill_parent"
                android:background="@drawable/rounded_corner"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="93dp"
                android:layout_marginEnd="93dp"
                android:elevation="3dp"/>

maybe I'm not compiling correctly Lollipop for pre-lollipop devices, Any suggestions?

If you need to see other parts of the code, I'll edit the question

Thanks

Salcido answered 29/12, 2014 at 18:7 Comment(2)
late comment.. But surely helps to anyone.. try this - https://mcmap.net/q/138643/-android-appcompat-21-elevationWenona
Thanks @RanjithKumar, surely helps!! nice job.Salcido
A
15

Elevation requires the device to run Lollipop. See this answer on how to simulate elevation https://mcmap.net/q/138643/-android-appcompat-21-elevation

Archenteron answered 29/12, 2014 at 18:39 Comment(0)
C
21

UPDATED ::

  1. Best Practice to do that is

    <android.support.v7.widget.CardView>
        <YourLayout>
    </android.support.v7.widget.CardView>
    

    and add library for cardview

    dependencies {
       ...
       compile 'com.android.support:cardview-v7:21.0.+'
     }
    
  2. On Pre-Lollipop you can use this drawable

    android:background="@android:drawable/dialog_holo_light_frame"

    it will give you the look of elevation

  3. you can create your own like this

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
    <shape android:shape="rectangle">
        <solid android:color="#BDBDBD"/>
        <corners android:radius="5dp"/>
    </shape>
    </item>
    
    <item
    android:left="0dp"
    android:right="0dp"
    android:top="0dp"
    android:bottom="2dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
        <corners android:radius="5dp"/>
    </shape>
    </item>
    </layer-list>
    

reference

Clemenciaclemency answered 10/3, 2016 at 12:4 Comment(0)
A
15

Elevation requires the device to run Lollipop. See this answer on how to simulate elevation https://mcmap.net/q/138643/-android-appcompat-21-elevation

Archenteron answered 29/12, 2014 at 18:39 Comment(0)
G
12

You can also use a CardView from the support library to implement surfaces.
To do so add a dependency to your build.gradle:

compile 'com.android.support:cardview-v7:23.1.1'

And then simply use it in your layouts:

  <android.support.v7.widget.CardView
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:layout_margin="16dp"
      android:background="#fff"
      >
  </android.support.v7.widget.CardView>

Here you have a quite more options to customise it comparing to using @android:drawable/dialog_holo_light_frame as a background

EDIT:
Also notice, that this approach allows you to simply implement
Material Design on Pre-Lolipop devices.
You can change the elevation,
round the corners etc.
To do so you have to:

 app:cardElevation="8dp"
 app:cardCornerRadius="8dp"
 app:contentPadding="5dp">

And not forget to add xmlns:app="http://schemas.android.com/apk/res-auto" to the root layout.

Also you can easily change the elevation in your code:

CardView card = (CardView) findViewById(R.id.yourPreetyCoolCardView);
card.setCardElevation(getResources()
    .getDimension(R.dimen.card_picked_up_elevation));

Use 8dp for picked up and 2dp for resting (usual) state and you will be awesome.

Granulite answered 1/4, 2016 at 12:38 Comment(0)
S
1

As described here, you can mimic the elevation on pre-lollipop with:

 android:background="@android:drawable/dialog_holo_light_frame"
Scherer answered 18/11, 2018 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.