Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:21+00:00 2026-05-27T05:51:21+00:00

I have recently built an application that shows you a camera preview on screen.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:51:21+00:00Added an answer on May 27, 2026 at 5:51 am

    setContentView override 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:

    ....
    setContentView(R.layout.main);
    ....
    final ViewGroup locationForMyPreview = (ViewGroup) findViewById(R.id.preview_location);
    mPreview = new Preview(this);
    locationForMyPreview.addView(mPreview);
    ....
    

    This R.id.preview_location ViewGroup can be defined as any type of layout in your main layout (RelativeLayout, LinearLayout, whatever).

    Your main.xml becomes something like:

    <?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 android:id="@+id/preview_location"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently written an application(vb.net) that stores and allows searching for old council
I have application reads in data from text file. Recently I realized that I
This one is a long story: i have recently built a win32 application in
I have recently taken over an application that uses the Peter Blum Date and
We have an application built on top of SQL server 2005 that we have
I have recently started work on an application that is already deployed to production.
In my Qt-based application (built using PyQt 4.8.6), I have a class that is
I'm working on application built years ago, that has recently stopped working correctly. Old
We have recently migrated a large, high demand web application to Tomcat 5.5 from
I have built a blog application using Ruby on Rails. In the application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.