43

I want to use an elevation in a custom layout element and I want to set in XML the value of that element using the default action bar elevation value. I can't find a way to get it in XML and I'd like to avoid to call getElevation() in code. Any tips?

greywolf82
  • 21,813
  • 18
  • 54
  • 108

5 Answers5

69

Default value is 8dp, create dimen with this value and use on whole project, I got it from sources, but the material design guidelines state the action bar elevation should be 4dp. enter image description here

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
Kirill Shalnov
  • 2,216
  • 1
  • 19
  • 21
  • It doesn't exist somewhere? How did you find this value? – android developer Feb 03 '15 at 11:13
  • @KirillShalnov I see. How come setting the elevation on a toolbar that was set as the action bar - doesn't do anything? I've tried setting it to 0 on some cases and 8dp on others, but it doesn't seem to do anything... – android developer Feb 03 '15 at 12:24
  • @androiddeveloper you can change elevetion programmaticaly on Lollipop. To older version you should to `null` in your style.xml to disable shadow, I understand the question? – Kirill Shalnov Feb 03 '15 at 12:35
  • @KirillShalnov Can you please try it? For some reason, when using the support library's toolbar (and use setSupportActionBar), I can't find out how to hide/show its shadow via code. "setElevation" doesn't work on both the toolbar and the action bar (on Lollipop). – android developer Feb 03 '15 at 14:29
  • @androiddeveloper `Toolbar toolbar = (Toolbar) findViewById(R.id.la_toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setElevation(0);` I tried. No shadow. may be if you have shadow it is not from toolbar, set windowContentOverlay - null – Kirill Shalnov Feb 03 '15 at 16:10
  • @KirillShalnov Sorry, I forgot I used my own shadow solution, so it doesn't do anything when using an elevation. I used a layout below the toolbar, which is an imageView for Lollipop and above (with some drawable), and a FrameLayout with android:foreground="?android:windowContentOverlay" for before Lollipop. Hiding and showing the shadow there is by hiding/showing this view. I've even made a post about it here: http://stackoverflow.com/q/27474766/878126 – android developer Feb 04 '15 at 12:17
  • 6
    @KirillShalnov The material design guidelines state the action bar elevation should be 4dp: http://www.google.com/design/spec/what-is-material/objects-in-3d-space.html#objects-in-3d-space-elevation – ashughes Mar 22 '15 at 00:59
  • @ashughes I got it from sources – Kirill Shalnov Mar 22 '15 at 11:50
  • If the elevation property is not working, it's a known issue. You need to set your view to have a solid background before it works: https://code.google.com/p/android/issues/detail?id=78248 – Pier Betos Jun 11 '15 at 03:57
  • The default value is clearly 4dp, not 8dp. I don't know which "sources" you checked, but the [dimens.xml in the appcompat design library](https://android.googlesource.com/platform/frameworks/support.git/+/master/design/res/values/dimens.xml#56) clearly states `4dp` – Christian García Aug 04 '16 at 09:47
  • @ChristianGarcía it was two years ago. Do you think for two years nothing has changed, but I still remember where I spied on these values? – Kirill Shalnov Aug 04 '16 at 13:21
  • 1
    I confirm that the default ActionBar elevation in AppCompat is still 8dp: https://android.googlesource.com/platform/frameworks/support.git/+/master/v7/appcompat/res/values/styles_base.xml#42 But the App Bar elevation in the design support library is 4dp, so there is definitely an inconsistency here. – BladeCoder Aug 06 '16 at 10:14
  • 6
    After some investigation, it turns out the default action bar elevation in the OS material themes changed from 8dp (Android 5) to 4dp (Android 6). I raised an issue on the Android bug tracker and the next version of AppCompat will use a default value of 4dp for the default ActionBar instead of 8dp currently. https://code.google.com/p/android/issues/detail?id=219400 – BladeCoder Aug 10 '16 at 00:33
  • @BladeCoder please, edit answer, and I will apply changes – Kirill Shalnov Aug 10 '16 at 18:06
13

The standard elevation of the app bar is 4dp according to material design spec. Here is the link: https://material.io/guidelines/material-design/elevation-shadows.html#elevation-shadows-shadows

Adi_28
  • 131
  • 1
  • 4
4

The default elevation of the action bar is 4dp. I did a little experiment by creating one custom view right below the action bar and set its elevation to 4dp. So, both were looking the same. Official documentation also mentioned 4dp standard elevation for the action bar.

  1. Nav drawer: 16dp
  2. App bar: 4dp
  3. Card: 1dp to 8dp
  4. FAB: 6dp
  5. Button: 2dp to 8dp
  6. Dialog: 24dp

Here App bar refers to Action bar.

Reference: Elevation

Stack Overflow
  • 1
  • 5
  • 23
  • 51
2

Here is the official resource available for the elevations.

Nithin Prasad
  • 554
  • 1
  • 9
  • 22
0

The default elevation of AppBarLayout is @dimen/design_appbar_elevation (which is 4dp). Actionbar has @dimen/abc_action_bar_elevation_material as default elevation (which is 4dp as well).

These values can be found in stateListAnimator of Widget.Design.AppBarLayout and the parent of Widget.AppCompat.ActionBar respectively. The Toolbar doesn't seem to have any default elevation.

Cristan
  • 12,083
  • 7
  • 65
  • 69