0

My android app takes some time to initialize, and I'd like to show a splash image before the loading screen appears and hide it afterwards. I searched through stackoverflow and found some solutions. I tried to follow this tutorial, that explains how to implement a proper splash screen that starts within a splash activity, but it didn't solve my problem, because there was still a several seconds black screen between the splash screen and the loading screen (which renders from a separate thread of C++ code, and has to initialize a bunch of things before render starts, please don't ask to change that part, it's a crossplatform C++ engine). Next I experimented with a ProgressDialog taken from here, started it in onCreate of the main activity and hided when C++ part starts actual rendering, and it worked fine except not being a splash image. But the timing was exactly what I need. Then I replaced it with an ImageView and it didn't work (no image is shown).

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    //...
    mImageView = new ImageView(this);
    mImageView.setScaleType(ScaleType.FIT_XY);
    mImageView.setImageResource(R.drawable.splash_bg);
    setContentView(mImageView);
}

splash_bg.png is put into res/drawable folder and shows fine from the splash activity. What is missing?

Community
  • 1
  • 1
LawfulChaotic
  • 91
  • 2
  • 7

3 Answers3

1

You should made theme for your splash activity like:

<style name="AppTheme.Splash" parent="YOURMAIN_THEME">
        <item name="android:windowBackground">@drawable/splash_bg</item>
</style>

And create your splash in drawable directory splash_bg.xml like:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/splash_background"/>
    <item
        android:top="30dp">
        <bitmap
            android:gravity="top"
            android:src="@drawable/demo_logo"
            />
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/demo_emlogo"/>
    </item>
</layer-list>
Alexander
  • 497
  • 6
  • 19
0

Add this line in your onCreate(Bundle savedInstance) method:

mImageView = new ImageView(this);
    mImageView.setScaleType(ScaleType.FIT_XY);
    mImageView.setImageResource(R.drawable.splash_bg);
LayoutParams imageViewLayoutParams 
         = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mImageView.setLayoutParams(imageViewLayoutParams);
Lips_coder
  • 686
  • 1
  • 5
  • 17
0
xml for splash screen.  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_splash_screen" />

</RelativeLayout>

In MainActivity 



public class Splash extends Activity {

    private final int SPLASH_DISPLAY_LENGHT = 1000;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splashscreen);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(Splash.this,
                        MainActivity.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}
  • This way splash shows for one second before the main activity starts, then the black screen shows again. So it simply adds another second of waiting to the user. Splash screen need to be visible during initialization, not before it. Also, instead of fixed SPLASH_DISPLAY_LENGHT time I need a way to dismiss the splash screen from the main activity when it starts actual rendering. – LawfulChaotic Apr 29 '16 at 06:54
  • Try to increase the size of splash screen display – Khumash Hussain May 02 '16 at 09:29