title: Xamarin Android - FrameLayout display only it's background when i add view first time, but displayed when i add it again
body: <p>I have a very strange problem. I got a custom FrameLayout (that just has a string FileName binded with mvvmcross and few methods ). 
  I use it to display medias (Pdf, videos, sounds and pictures) on item click.
  My items being small icons representing my media files and binded to mediaItems.</p>
  
  <p>When i click on one of those icons everything is doing well when i hit my breakpoints, the framelyout appears, but it's just displaying it's background and not it's child view.</p>
  
  <p>If i reduce it and click again on the very same item, the framelayout will appear again and show my content properly and i can't figure out why.</p>
  
  <p>To understand the logic behind it here is how it works:</p>
  
  <ul>
  <li><p>I click on one of my items</p></li>
  <li><p>In my viewmodel it raise the onclick command, and set my Property SelectedMedia</p></li>
  <li><p>The view containing my custom frameLayout, get its visibility from gone to visible as it's visibility is binded to SelectedMedia (if null=>gone if not => visible)</p></li>
  <li><p>At the same time my custom FrameLayout got its property FileName (which is binded on the Property SelectedMedia.FileName) setted.</p></li>
  <li><p>The FileName setter (as you can see in the code saple) calls my Init method, whoose job is to select which kind of view to add to my custom FrameLayout and then to add it</p></li>
  <li><p>Finally my container appears, within it there is my custom FrameLayout, but i can only see it's background and not its child view and i don't know why. </p></li>
  </ul>
  
  <p>Here is my custom framelayout C# code :</p>
  
  <pre><code>public class MediaController : FrameLayout
  {
  
      #region [ Fields ]
      private Context _context;
      private ImageController _imageController;
      private SoundAndVideoController _soundAndVideoController;
      private PdfController _pdfController;
      private LayoutInflater _layoutInflater;
      private readonly string[] supportedVideoFormat = { "mp4", "3gp", "mkv", "mpg" };
      private readonly string[] supportedPictureFormat = { "png", "bmp", "gif", "jpg" };
      private readonly string[] supportedAudioFormat = { "flac", "mp3", "wav" };
      private readonly string[] supportedPdfFormat = { "pdf" };
      #endregion
      #region [ Properties ]
      private string _fileName;
      public string FileName
      {
          get
          {
              return _fileName;
          }
          set
          {
              if (value != null)
              {
                  _fileName = value;
                  Init();
              }
          }
      }
      #endregion
  
      #region [ Constructors ]
      public MediaController(Activity activity) : base(activity)
      {
          _context = activity.BaseContext;
          Init();
      }
  
      public MediaController(Context context) : base(context)
      {
          _context = context;
          Init();
      }
  
      public MediaController(Context context, IAttributeSet attrs) : base(context, attrs)
      {
          _context = context;
          Init();
      }
  
      public MediaController(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
      {
          _context = context;
          Init();
  
      }
  
      public MediaController(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
      {
          _context = context;
          Init();
      }
      #endregion
      #region [ Methods ]
      public void InitViews()
      {
  
      }
  
      private void Init()
      {
          if (!string.IsNullOrEmpty(FileName) &amp;&amp;_context != null)
          {
              this.RemoveAllViews();
              string extension = FileName.Split('.').Last();
              _layoutInflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
              if (supportedAudioFormat.Any(extension.ToLower().Contains) || supportedVideoFormat.Any(extension.ToLower().Contains))
              {
                  InitMusicOrVideoPlayer(extension);
              }
              else if (supportedPictureFormat.Any(extension.ToLower().Contains))
              {
                  InitImageViewer();
              }
              else if (supportedPdfFormat.Any(extension.ToLower().Contains))
              {
                  InitPdfViewer();
              }
              else
              {
                  //manage error;
              }
          }
      }
  
      private void InitMusicOrVideoPlayer(string fileExtension)
      {
          SurfaceView musicView = (SurfaceView)layoutInflater.Inflate(Resource.Layout.MusicVideoTemplate, null);
  
          FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(this.Width, this.Height);
  
          this.AddView(musicView, layoutParams);
  
          if (supportedAudioFormat.Any(fileExtension.ToLower().Contains))
          {
              ImageView imageView = new ImageView(_context);
  
              imageView.Background = _context.GetDrawable(Resource.Drawable.speaker);
  
              imageView.SetScaleType(ImageView.ScaleType.FitCenter);
              layoutParams = new FrameLayout.LayoutParams(this.Width / 4, this.Height / 4);
              layoutParams.SetMargins((int)(this.Width * 0.375f), (int)(this.Height * 0.375f), 0, 0);
              this.AddView(imageView, layoutParams);
              soundAndVideoController = new SoundAndVideoController(musicView, FileName, _context, true);
          }
          else
          {
              soundAndVideoController = new SoundAndVideoController(musicView, FileName, _context, false);
          }
      }
  
      private void InitPdfViewer()
      {
          FrameLayout pdfContainer = (FrameLayout)_layoutInflater.Inflate(Resource.Layout.PdfTemplate, null);
          FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(this.Width, this.Height);
          this.AddView(pdfContainer, layoutParams);
          _pdfController = new PdfController(FileName, pdfContainer, _context);
      }
  
      private void InitImageViewer()
      {
          FrameLayout mediaImageContainer = (FrameLayout)_layoutInflater.Inflate(Resource.Layout.MediaImageTemplate, null);
          FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(this.Width, this.Height);
          this.AddView(mediaImageContainer, layoutParams);
          _imageController = new ImageController(FileName, mediaImageContainer);
      }
  
      #endregion
  }
  </code></pre>
  
  <p>And here is my xml containing my custom framelayout :</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:local="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:layout_gravity="center"
      android:background="@color/Black"
      local:MvxBind="Visibility  MustShowMedias, Converter=Visibility "&gt;
      &lt;LinearLayout
          android:orientation="horizontal"
          android:weightSum="1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
          &lt;LinearLayout
              android:id="@+id/MediaLeftLayout"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="0.05"&gt;
              &lt;Button
                  android:id="@+id/MediaPreviousButton"
                  android:layout_width="match_parent"
                  android:layout_height="35dp"
                  android:layout_gravity="center"
                  android:text="Previous" /&gt;
          &lt;/LinearLayout&gt;
          &lt;LinearLayout
              android:id="@+id/MediaCenterLayout"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="0.9"
              android:orientation="vertical"
              android:weightSum="1"&gt;
              &lt;space
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="0.05" /&gt;
              &lt;MyProject.Droid.Views.MediaControllers.MediaController
                  android:id="@+id/MediaContainer"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="0.9"
                  android:background="#FFFFFF"
                  local:MvxBind="FileName SelectedMedia.Source"&gt;
  
              &lt;/MyProject.Droid.Views.MediaControllers.MediaController&gt;
          &lt;/LinearLayout&gt;
          &lt;LinearLayout
              android:id="@+id/MediaRightLayout"
              android:orientation="vertical"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="0.05"&gt;
              &lt;Button
                  android:id="@+id/MediaCloseButton"
                  android:layout_width="match_parent"
                  android:layout_height="35dp"
                  android:textColor="#FF0000"
                  android:text="X"
                  local:MvxBind="Click CloseMediaWindowCommand" /&gt;
              &lt;Button
                  android:id="@+id/MediaNextButton"
                  android:layout_width="match_parent"
                  android:layout_height="35dp"
                  android:layout_gravity="center"
                  android:text="Next" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>I'm quite new at Android development and i have absolutely no idea why this not working. Maybe my frame layout isn't fully loaded (wich is weird because it's in a include in my View). I would greatly accept any kind of help on this problem !</p>
  
  <p>Thanks</p>
  
  <p>Edit : 
  Okay here are some updates:</p>
  
  <ul>
  <li>I tried to use BringChildToFront, setFocus, to set the view containing my framelayout tu clickable (as i noticed my clicks were going through ).</li>
  </ul>
  
  <p>Edit 2 :</p>
  
  <ul>
  <li><p>I updated my code (still not working as expected)</p></li>
  <li><p>I tried to set my layout on constructor call, wich just broke the fact that the view dsiplay the second time i try to load it, letting me with a blank framelayout, so i get back to the old way</p></li>
  </ul>
  
  <p>PS: Sorry for my terrible english</p>