Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android elevation and setElevation not the same effect

I noticed when I use elevation property in xml and set it to 4dp, I get a normal shadow. When I use setElevation(4) in java, I get less of a shadow than the xml property does. How can I fix that?

like image 879
qwertz Avatar asked Jun 07 '15 11:06

qwertz


1 Answers

The answer given by Anton Kovalyov is almost correct, but it is actually the other way around. To get the correct elevation you need to convert your dp to pixels and feed it into setElevation, so the correct answer looks like this:

setElevation(4 * context.getResources().getDisplayMetrics().density);

setElevation takes a pixel value. User TinTran's comment is correct.

like image 136
Bernd Kampl Avatar answered Sep 27 '22 17:09

Bernd Kampl