I have recently built an application that shows you a camera preview on screen.
package com.street.lamp;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
public class StartingPoint extends Activity {
private Preview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("hoera");
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
// Create our Preview view and set it as the content of our activity.
try{
mPreview = new Preview(this);
setContentView(mPreview);
System.out.println("hoera");
}
catch(RuntimeException e){
System.out.println(e.getMessage());
}
}
}
// ----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Above this is my main screen code, and as you can see i have setContentView twice.
now, i know that setting it twice will only show the latest but i haven’t found any other solution. this is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText android:layout_width="match_parent" android:text="Hello World" android:textColor="@android:color/white" android:focusable="true" android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_weight="0.18" android:background="@android:color/transparent">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
How do i include both contentviews?
Thanks!
setContentViewoverride the whole content for your current activity. If you want a layout that includes some dynamic components (like your preview), then the preview needs to be part of the main layout, and be added at runtime in the specific component you defined.Something like:
This
R.id.preview_locationViewGroupcan be defined as any type of layout in your main layout (RelativeLayout,LinearLayout, whatever).Your
main.xmlbecomes something like: