Why XML contents not showing on the app when using FrameLayout in Android

599 Views Asked by At

In my app I have created a custom camera app ,using FrameLayout in Xml. And I have a capture button to capture images,and two other buttons. These buttons are arranged inside a RelativeLayout inside FrameLayout.But it is not displaying when running my App. Any solution for this problem. Can anybody help?

XML file

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

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <ImageButton
        android:id="@+id/buttonLoadPicture2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:padding="6dp"
        android:scaleType="fitCenter"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>



    <ImageButton
        android:id="@+id/button_capture"
        android:layout_width="90dip"
        android:layout_height="50dip"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"

        android:layout_marginTop="0dp"
        android:layout_toRightOf="@+id/buttonLoadPicture2"
        android:layout_alignParentBottom="true"
        android:src="@drawable/camera"
        android:layout_marginRight="5dp"



        />
    <Button android:id="@+id/buttonLoadPicture3"
        android:layout_width="90dip"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end"
        android:layout_toRightOf="@+id/button_capture"

        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:text="Cancel"
        ></Button>
    </RelativeLayout>

</FrameLayout>
</LinearLayout>

JAVA CODE

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview mPreview;
public static final int MEDIA_TYPE_IMAGE = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
    System.out.println("Starting!");

    // Create an instance of Camera
    mCamera = getCameraInstance();
    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    final Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

            if (pictureFile == null){
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), pictureFile.getAbsolutePath(), pictureFile.getName(), pictureFile.getName());
            } catch (FileNotFoundException e) {

            } catch (IOException e) {

            }
        }
    };





    // Add a listener to the Capture button
    captureButton.setOnClickListener(

            new View.OnClickListener() {

                public void onClick(View v) {
                    // get an image from the camera

                    System.out.println("Photo Taking!");
                    mCamera.takePicture(null, null, mPicture);



                }
            }
    );



}




/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
protected void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}



private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}



/** Create a File for saving an image or video */
private  File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp");


    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

}

1

There are 1 best solutions below

0
On

Your FrameLayout covers the entire screen and you are adding your camera preview to it which will be added on top of the buttons and therefore cover them. A quick fix would be move all your buttons out of the frame layout so your layout looks like this.

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

    <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">


    </FrameLayout>
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <ImageButton
                android:id="@+id/buttonLoadPicture2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="6dp"
                android:scaleType="fitCenter"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"/>



        <ImageButton
                android:id="@+id/button_capture"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"

                android:layout_marginTop="0dp"
                android:layout_toRightOf="@+id/buttonLoadPicture2"
                android:layout_alignParentBottom="true"
                android:src="@android:drawable/ic_menu_camera"
                android:layout_marginRight="5dp"/>

        <Button android:id="@+id/buttonLoadPicture3"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true"
                android:layout_gravity="end"
                android:layout_toRightOf="@+id/button_capture"

                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:text="Cancel"
                ></Button>
    </RelativeLayout>
</LinearLayout>

Note that the above will keep your camera preview above the relative layout containing the buttons. So if you are trying to have the buttons on top your preview you can retain your old code/layout and use brintToFront()