2

Now how to tell for the SurfaceView to be square, have the same height as ImageView's width?

<RelativeLayout
    android:id="@+id/rlQr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_below="@+id/rlTop">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivScanTop"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/qr_margin_top" />

    <SurfaceView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/ivScanTop"
        android:layout_alignRight="@+id/ivScanTop"/>
Moni
  • 65
  • 1
  • 7

3 Answers3

1

Based on this answer you should extend the SurfaceView and add that one on the .xml file. In that case the UI designer will show a square.

Community
  • 1
  • 1
matheszabi
  • 594
  • 5
  • 16
0

You can do it in your onCreate method, in Java.

  1. You have to add an id to SurfaceView

You have to add this inside your SurfaceView XML declaration:

android:id="@+id/surfaceViewId"
  1. In your activity java class, inside your onCreate method, try this:

Inside your onCreate method:

SurfaceView surface = (SurfaceView) findViewById(R.id.surfaceViewId);
ImageView image = (ImageView) findViewById(R.id.ivScanTop);
surface.getHolder().setFixedSize(image.getWidth(), image.getHeight());
0

Initialize your views and then add this to your Activity's onCreate method :

ivScanTop.post(new Runnable() {
        @Override
        public void run() {
        surfaceView.getHolder().setFixedSize(ivScanTop.getwidth(), ivScanTop.getwidth())  ;                       
    }
});
Alikbar
  • 685
  • 6
  • 20