Search code examples
androidcamerapreviewtextview

Is it possible to have Camera Preview and TextView on the same screen?


I was wondering if there is some way to have a camera preview fill a part of the screen, and at the bottom have a textview box into which I can add text later. I don't have any code yet because my team is still evaluating if this is possible at all before proceeding with the code.

Edit 2: Now I am finally able to see the text on the screen after playing around with android:gravity. But the TextView always shows up on the top of the screen, is there some way to move it to the bottom?

Edit 3: Changing android:layout_height to fill_parent instead of wrap_content fixed the positioning issue

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
      <SurfaceView android:id="@+id/preview_view"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_centerInParent="true"/>
      <com.google.zxing.client.android.ViewfinderView
           android:id="@+id/viewfinder_view"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="@color/transparent"/>
      ........
      ........
      <TextView android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:gravity="bottom" 
           android:text="Header text"/>

</FrameLayout>

Solution

  • Yes, this is possible. You should use FrameLayout for this. Here is an example:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
    
      <SurfaceView android:id="@+id/preview_view"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:layout_centerInParent="true"/>
    
      <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center_horizontal" 
                android:text="Header text"/>
    
    </FrameLayout>