I'm using the CameraPreview example API demo. I need to add some views (button, etc..) overlaying the SurfaceView.
For this, I'm trying to set their parameters, but they appear all the time on the top-left side of the screen.
This is the onCreate method of the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnTakePhoto = new Button(this);
btnTakePhoto.setBackgroundResource(android.R.drawable.ic_menu_camera);
/*Set container*/
mPreview = new Preview(this);
setContentView(mPreview);
/*Set button params and add it to the view*/
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
addContentView(btnTakePhoto, buttonParams);
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
defaultCameraId = i;
}
}
}
Have to say that the values here
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
are updating well if I change them. What doesn't change is what contains the addRule() method
are you wanna to add the btnTakPhoto to the right center of the view? if so, have a try:
Try This
Method to get screen height and width
You must be targeting an API older than 14, setParent should work with newer API's. Above code is an alternative way, however you would have to place everything by code, but it works. Hope this helps.
FORM is the container, and you would have to add your other components like FORM.addView(btn) with relative layout params.
Finally solved. When doing setContentView() and addContentView(), I was placing the views in a DecorView which is a FrameLayout. So, LayoutParams referencing RelativeLayout won't work, as for a FrameLayout only generic features of LayoutParams will work.
So, the thing is to first create a relativeLayout, set the params and set it as the content:
But, now, every time I want to add a view, I have to add it to this relativeLayout this way:
Just this.
try to set the parent of btnTakePhoto to mPreview
which will cause the button to appear above the preview.