Problem
Using RelativeLayout.LayoutParams
and marginTop
before setLayoutParams( params )
.
Works on all devices for half a second but some cases it bumps back to top.
The view stays centered in the holding view on my Moto X 2014 running Lollipop but not on my Nexus 4 running Lollipop.
Opening activity
- Opens activity
- The margin is fine and the SurfaceView is centered
- ~200ms delay
- The margin resets and its back to top (top of SurfaceView at the top of holder)
Closing activity
- Back pressed
- ~200ms delay
- The margin sets in, putting my view to the right position
- Activity closes
Code (Edited)
RelativeLayout holder = ( RelativeLayout ) findViewById( R.id.holder );
RelativeLayout.LayoutParams params = ( RelativeLayout.LayoutParams ) holder.getLayoutParams();
CustomCamera view = ( CustomCamera ) findViewById( R.id.surface ); // Extends SurfaceView
params.topMargin = margin;
view.setLayoutParams( params );
Example
I need the margin to work like this every time on every device. On some devices the red (SurfaceView) is aligned with top of screen ignoring the margin and gravity.
To make your life easier, here is my simple camera implementation which can help you out. Note that this implementation relays on the old android.hardware.Camera API. Starting API level 21 there is a new way of working with the camera.
Anyway here is your basic xml file for your activity:
Your camera activity:
Your needed utils classes. You can add these to your own CameraActivity if you won't use them elsewhere:
CameraUtils:
ScreenUtils:
And last but not least, don't forget about you permissions in the manifest file:
I hope this answer helps you to get your camera on the go!
Cheers, Mike