0

i have a custom surfaceview which i need to add into my current main.xml layout. this surfaceview is used to stream camera liveview. i having issues making this work.

i have no issue if i run the code as

     cameraPreview = new HttpCameraPreview(this, null, viewWidth, viewHeight);
setContentView(cameraPreview);

however, i do not want my custom surfaceview to be the main view. i want it to be a surfaceview inside my main.xml.

hope to get some suggestions/advises. i tried refering to this link, but no help too. Draw SurfaceView from layout xml

thanks!

part of the main.xml as below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
                    <ImageView
               android:id="@+id/preview_image_view"
               android:layout_height="wrap_content"
               android:layout_weight="3.0"
                   android:layout_width="fill_parent"
               />
<SurfaceView Class ="org.saboteur.nikonshooter.HttpCameraPreview"
android:layout_width="fill_parent" 
android:id="@+id/surface_preview" 
android:layout_height="10dip" 
android:layout_weight="3"/>

my custom surfaceview class partial code look like this:

  public class HttpCameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "test";
    private static final String url = "http://bijint.com/jp/tokei_images/0022.jpg";
    private CanvasThread canvasThread;

    private SurfaceHolder holder;
    private HttpCamera camera;

    private HttpCameraPreview mSurfaceView;

    private int viewWidth;
    private int viewHeight;

    public HttpCameraPreview(Context context, AttributeSet attributeSet, int viewWidth, int viewHeight) {
        super(context, attributeSet);
        Log.e("HttpCameraPreview", "inside HttpCameraPreview()");
        Log.e("HttpCameraPreview", "create surface view to r.id");


        try{
        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        this.viewWidth = viewWidth;
        this.viewHeight = viewHeight;
        canvasThread = new CanvasThread();
        catch (Exception e){
            Log.e("HttpCameraPreview", "--->Exception = "+e);
        }
    }
Community
  • 1
  • 1
keithwb
  • 81
  • 1
  • 4
  • 11

1 Answers1

-1

Why not add the HttpCameraView to a layout in your main.xml programatically. Give your LinearLayout an id like "@+id/parent_view" and then call that by

LinearLayout parentView = (LinearLayout) findViewById(R.id.parent_view);
cameraPreview = new HttpCameraPreview(this, null, viewWidth, viewHeight);
parentView.addView( cameraPreview );
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • You probably need an attribute set. Give it some LinearLayout.LayoutParams instead of null because every view needs a layout_width and layout_height – AJcodez Feb 02 '12 at 07:30
  • hi, must i pass an attribute set, can i just pass context only? as i not sure what attribute set to pass. – keithwb Feb 02 '12 at 07:45
  • You can only pass context, but then you need to set the layout parameters in the custom class. http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html – AJcodez Feb 02 '12 at 17:35