title: Android splash screen ImageView not loading
body: <p>My Android application is supposed to show a splash screen consisting solely of an ImageView while it is still loading. The ImageView should just display a local resource. Here is my code:</p>
  
  <pre><code>public class GameActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          ImageView splash = new ImageView(this);
          splash.setImageResource(R.drawable.splashImage);
  
          RelativeLayout layout = new RelativeLayout(this);
          layout.setId(R.id.gameView);
          uilayout addView(splash, someLayoutParams);
          setContentView(layout, someOtherLayoutParams);
          // with a return-statement, the image would be displayed now
  
          final GameActivity activity = this;
          this.findViewById(R.id.gameView).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  // this is supposed to wait until the image has been fully loaded
                  activity.init(this);
              }
          });
      }
  
      public void init(ViewTreeObserver.OnGlobalLayoutListener listener) {
          try {
              findViewById(R.id.gameView).getViewTreeObserver().removeOnGlobalLayoutListener(listener);
          } catch (NoSuchMethodError x) {
              findViewById(R.id.gameView).getViewTreeObserver().removeGlobalOnLayoutListener(listener);
          } finally {
              LinearLayout layout = new LinearLayout(this);
              // lots of stuff
              setContentView(layout);
          }
      }
  }
  </code></pre>
  
  <p>My problem is that as soon as I execute code after the first call for setContentView(), my <strong>splash screen image is not displayed</strong> and the new layout immediately shows up.
  I would appreciate any help on how to solve this problem.</p>
  
  <p><strong>EDIT:</strong>
  It was my fault to assume that my code was the ideal solution for a splash screen. <a href="https://www.bignerdranch.com/blog/splash-screens-the-right-way/" rel="nofollow">This</a> tutorial really helped me out. My solution based on this tutorial:</p>
  
  <p><em>@drawable/layer-list_splash</em>:</p>
  
  <pre><code>&lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item
          android:drawable="@color/color_background"/&gt;
  
      &lt;item&gt;
          &lt;bitmap
              android:gravity="center"
              android:src="@drawable/sprite_splash"/&gt;
      &lt;/item&gt;
  &lt;/layer-list&gt;
  </code></pre>
  
  <p>and my <em>styles.xml</em>:</p>
  
  <pre><code>&lt;resources&gt;
      &lt;style name="AppTheme" parent="Theme.AppCompat.NoActionBar"&gt;
          &lt;item name="android:windowBackground"&gt;@drawable/layers_splash&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>