title: Xamarin Android how to set Surface texture listener
body: <p>I am working in my app, it should open a camera stream and take a photo when the user press a button, this is my axml file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"&gt;
      &lt;TextureView
          android:id="@+id/textureView"
          android:layout_marginTop="-95dp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
      &lt;Button
          android:id="@+id/takePhotoButton"
          android:layout_width="65dp"
          android:layout_height="65dp"
          android:layout_marginBottom="15dp"
          android:layout_gravity="center|bottom"
          android:background="@drawable/TakePhotoButton" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>And this is my code:</p>
  
  <pre><code>using System;
  
  using Android.App;
  using Android.OS;
  using Android.Views;
  using Android.Widget;
  using Android.Hardware;
  using Android.Graphics;
  
  namespace CameraStream
  {
      [Activity(Label = "CameraStream", MainLauncher = true, Icon = "@drawable/icon")]
      public class Activity1 : Activity, TextureView.ISurfaceTextureListener
      {
          Android.Hardware.Camera _camera;
          TextureView _textureView;
  
          protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
  
              _textureView = new TextureView(this);
              _textureView.SurfaceTextureListener = this;
  
              SetContentView(_textureView);
          }
  
          public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)
          {
              _camera = Android.Hardware.Camera.Open();
  
              var previewSize = _camera.GetParameters().PreviewSize;
              _textureView.LayoutParameters =
                  new FrameLayout.LayoutParams(h, w, GravityFlags.Center);
              try
              {
                  _camera.SetPreviewTexture(surface);
                  _camera.StartPreview();
              }
              catch (Java.IO.IOException ex)
              {
                  Console.WriteLine(ex.Message);
              }
              _textureView.Rotation = 90.0f;
  
          }
  
          public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface)
          {
              _camera.StopPreview();
              _camera.Release();
  
              return true;
          }
  
          public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height)
          {
              // camera takes care of this
          }
  
          public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>as you can notice, it wont show the main axml, how can I merge the code  that opens the camera and starts the stream with my buttons?
  I tryed this:</p>
  
  <pre><code> protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
  
              _textureView = FindViewById&lt;TextureView&gt;(Resource.Id.textureView);
              _textureView.SurfaceTextureListener = this;
  
              SetContentView(Resource.Layout.Main);
          }
  </code></pre>
  
  <p>but an exception message shows up, I found a code similar to  the one I need on the answer of  this question: <a href="https://stackoverflow.com/questions/37097774/xamarin-android-display-a-stream-from-the-camera">Xamarin Android Display a stream from the camera</a></p>
  
  <p>but there the button starts and finishes the stream, and I want the stream always working, how can I do that ?
  Thanks.</p>