Le Parkour
Le Parkour

Reputation: 97

Android button missing after add content view

I have a frame layout with a few buttons:

setContentView(R.layout.main_layout);

After I call this camera function,

addContentView(mPreview, newFrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

It's totally shielded my XML layout. I want the button on top of my camera view, how can I do it?

This is my camera class:

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

Camera mCamera;
boolean isPreviewRunning = false;

CameraSurfaceView(Context context) {
    super(context);
    SurfaceHolder mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    synchronized(this) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.e("Camera", "mCamera.setPreviewDisplay(holder);");
        }
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();

    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    synchronized(this) {
        try {
            if (mCamera!=null) {
                mCamera.stopPreview();
                isPreviewRunning=false;
                mCamera.release();
            }
        } catch (Exception e) {
            Log.e("Camera", e.getMessage());
        }
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}

}

This is my XML layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Background"
    android:id="@+id/background"
    android:onClick="background"
    android:layout_gravity="left|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Camera"
    android:id="@+id/camera"
    android:onClick="camera"
    android:layout_gravity="right|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/save"
    android:onClick="save"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

Upvotes: 1

Views: 627

Answers (1)

Rahul Tiwari
Rahul Tiwari

Reputation: 6968

This is because view added at the end comes at top in framelayout.

I suggest you to make a container(any layout) in your frame layout at start of the layout and put your camera view in that container.

something like :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/cameracontainer"></LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Background"
    android:id="@+id/background"
    android:onClick="background"
    android:layout_gravity="left|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Camera"
    android:id="@+id/camera"
    android:onClick="camera"
    android:layout_gravity="right|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/save"
    android:onClick="save"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

and in activity:

LinearLayout layout= (LinearLayout )findViewById(R.id.cameraContainer);
layout.addView(mPreview);

Upvotes: 1

Related Questions