Android SplashScreen
Asked Answered
P

6

81

I'm developing an application which basically downloads a lot of data at the start of the application itself and displays it in the ListActivity. What I'm planning to do is show a Splash Screen till the data is loaded.

Till now all my attempts have been futile. I tried anddev.org mentioned methods, but my problem is that the main Activity should start but The Splash Screen should be visible till I populate my ListActivity. So in short I have to go through the following steps:

  1. Start my main activity.
  2. Show the Splash Screen.
  3. Keep running the process in background.
  4. Exit the Splashscreen when processing completed and show the main List.

Hope you understand what it is like....

Pearson answered 30/12, 2009 at 10:43 Comment(2)
Can anybody explain y even a progress bar is not loading even if i try to create and start it in the OnCreate function of my Main List... and dismiss it after my process finishes... even this is not working... i mean is there any default methodology i need to follow... like starting the progress bar in OnStart() or something??Pearson
Did you try what the accepted answer in #2223390 says? EDIT: Ok, the accepted answer is kinda the same..Lanky
C
89

The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.

So your Activity's onCreate() method would simply create a ProgressDialog and show it. Then create the AsyncTask and start it. I would make the AsyncTask an inner class of your main Activity, so it can store the data it has downloaded to some variable in your Activity and close the ProgressDialog in its onPostExecute() method.

Not sure how to elaborate anymore without just showing the code, so here it is:

public class MyActivity extends Activity {
    private ProgressDialog pd = null;
    private Object data = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Show the ProgressDialog on this thread
        this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);

        // Start a new thread that will download all the data
        new DownloadTask().execute("Any parameters my download task needs here");
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {
         protected Object doInBackground(String... args) {
             Log.i("MyApp", "Background thread starting");

             // This is where you would do all the work of downloading your data

             return "replace this with your data object";
         }

         protected void onPostExecute(Object result) {
             // Pass the result data back to the main activity
             MyActivity.this.data = result;

             if (MyActivity.this.pd != null) {
                 MyActivity.this.pd.dismiss();
             }
         }
    }    
}

Obviously there are some pieces you need to fill in there, but this code should run and give you a good starting point (forgive me if there is a code error, I don't have access to the Android SDK as I'm typing this currently).

Some more good reading on the subject of AsyncTasks in Android can be found here and here.

Carollcarolle answered 30/12, 2009 at 19:35 Comment(7)
What do you mean by return "replace with data object" ? I have my two functions that I want to load in the asynctask like you said but I don't know what I am suppose to return. I tried to return null but the app crashes.Luxuriance
AsyncTask<String, Void, Object> where Object represents the data type you want to return. See the "AsyncTask's generic types" section here: developer.android.com/reference/android/os/AsyncTask.htmlCarollcarolle
This is old, I know, but I have tried implementing this and the ProgressDialog (in my case a regular Dialog that loads a layout) is never shown. I only see it if I comment out the .dismiss() in the onPostExecute. The first screen I see is still the MyActivity theme. After that it goes straight to R.layout.main.Outbalance
@Outbalance if .dismiss() is causing it to not display, then your AsyncTask is ending immediately. The method I outlined is for displaying a splash screen that shows while some long running startup task runs.Carollcarolle
@mbaird it's not ending immediately. I do indeed have a background process that takes 2-3 seconds to complete. For some reason though, nothing on the UI is updated until after the Async is done, thus defeating the purpose.Outbalance
@Outbalance the fact that the .dismiss() call is closing it immediately, and the .dismiss() call happens in onPostExecute() tells me you aren't running your background process as the AsyncTask. You need to make sure the doInBackground() method doesn't return until your background process is complete.Carollcarolle
is there a way of showing a full screen image (from drawables) instead of a ProgressDialog box?Vaccination
I
62

just for reference this is the best way I found to make a splash screen: http://android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html

I was searching for this for quite a while, from androids docs.. if you want to avoid those black screens, you need to create a theme with windowBackground so:

<resources>
    <style name="Theme.Shelves" parent="android:Theme">
        <item name="android:windowBackground">@drawable/background_shelf</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

And set this theme as the theme for your main activity... TADA, splashscreen from the first second.

If you want a complex background and not just an image that will be stretched to fill you can use Drawables, here is an example of a layer-list that will keep the logo centered with a black background:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@color/black">
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/logo"
            android:tileMode="disabled" >
        </bitmap>
    </item>
</layer-list>
Irra answered 14/8, 2011 at 14:18 Comment(10)
this link is dead, but the page content and source code is available on archive.org: web.archive.org/web/20101113015720/http://developer.android.com/…Blinkers
Is there a way to use a layout.xml instead of an drawable for android:windowBackground? I get a crash when trying to do so, but I wonder if there is a workaround.Outbalance
@Outbalance no there isn't. BUT you can use Drawables so if you want to have a Logo in the middle of the screen then you should use a layer-list drawable and use gravity to position your images. I'll update the answer with an example.Irra
@Ranaan Theme solution works for me, but how I should connect layer-list to the theme?Dotty
@Dotty item name="android:windowBackground" can either be a fixed color or image or a xml drawable like layer-list placed in your res/drawable folder. You can read about Drawables here: developer.android.com/guide/topics/resources/…Irra
@Ranaan I tried it, but my app crashes at start with an exception: Binary XML file line #18: <bitmap> requires a valid src attributeDotty
Well, what are you supplying in the bitmap src ? it should be an image that is available. (You should open a new question)Irra
@Ranaan You're right, I did: #22940732Dotty
This should be the accepted answer! From a lot of points of view this is more optimal than other solutions.Schoolroom
Yeah that is real splash screen. Thanks a tonMocambique
N
1
  1. Start my main activity.
  2. Show the Splash Screen.
  3. Keep running the process in background.
  4. Exit the Splashscreen when processing completed and show the main List.

I tried this way, but the problem is; it will show the main activity before starting the splash screen activity.

I made it this way:

  1. Start the Splash screen
  2. When process gets completed, start the "main activity"
  3. Do Not forget to handle the back button, so it should close the App ist will be pressed in the main activity. Otherwise will back to the splash screen (loop)

My Problem is how to disable "show the Menu of the Splash screen activity" by pressing the menu-button.

Edit:

Disable show menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // getMenuInflater().inflate(R.menu.activity_main, menu);
    return false;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {       
    // return super.onMenuItemSelected(featureId, item);
    return false;
}
Nehemiah answered 7/2, 2013 at 15:3 Comment(0)
C
1

Splash screen example :

public class MainActivity extends Activity {
    private ImageView splashImageView;
    boolean splashloading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splashImageView = new ImageView(this);
        splashImageView.setScaleType(ScaleType.FIT_XY);
        splashImageView.setImageResource(R.drawable.ic_launcher);
        setContentView(splashImageView);
        splashloading = true;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                setContentView(R.layout.activity_main);
            }

        }, 3000);

    }

}
Claymore answered 9/6, 2014 at 7:10 Comment(0)
E
0

I know that this question is pretty old, and the OP may not need it anymore. But I just want to add this answer to help people who may need this to archive a splash screen.

Answer (only work on Android Oreo or greater versions)


Actually, in the newer version of Android (after Android Oreo), it already support built-in splash screen implement. That means you don't need to create extra activity to do that. You only need a drawable resource file for display.

Using this way is faster to your splash screen and soon show your content just after the initialization. But please note that this only work on Android Oreo or greater versions. On the previous version, it will show white instead of your splash screen (at least I think so).

You need this line in your AppTheme style:

<item name="android:windowSplashscreenContent">@drawable/YOUR_SPLASH_SCREEN_DRAWABLE</item>

This is a full example:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <!-- Set your splash screen here, it accept a resource from drawable directory-->
    <item name="android:windowSplashscreenContent" tools:targetApi="o">@drawable/splash_screen</item>
</style>

Reference


And for more informations about this attribute, see the official reference here: https://developer.android.com/reference/android/R.attr#windowSplashscreenContent

As it said, it is added in API level 26.

And a short extract of what it said:

Reference to a drawable to be used as the splash screen content of the window. This drawable will be placed on top of the windowBackground with its bounds inset by the system bars. If the drawable should not be inset by the system bars, use a fullscreen theme.

Note that even if no splashscreen content is set on the theme, the system may still show a splash screen using the other attributes on the theme, like the windowBackground.

Eclipse answered 18/1, 2021 at 23:15 Comment(2)
That's just the same as you've posted there: https://mcmap.net/q/66381/-basic-android-launcher-splash-screenCupbearer
@Martin Zeitler I already deleted that answer because it doesn't related to that question. I moved it to here so it is more related.Eclipse
F
0

You should use the Android Platform SplashScreen API for Splash Screens which you can provide an adaptive icon or an AVD along with controlling how long it is visible for. Check out the documentation

 class MainActivity : Activity() {
    
       override fun onCreate(savedInstanceState: Bundle?) {
           // Handle the splash screen transition.
           val splashScreen = installSplashScreen()
    
           super.onCreate(savedInstanceState)
           setContentView(R.layout.main_activity)

and create a splash screen theme that you provide in the manifest

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
   <!-- Set the splash screen background, animated icon, and animation duration. -->
   <item name="windowSplashScreenBackground">@color/...</item>

   <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
        animated drawable. One of these is required. -->
   <item name="windowSplashScreenAnimatedIcon">@drawable/...</item>
   <!-- Required for animated icons -->
   <item name="windowSplashScreenAnimationDuration">200</item>

   <!-- Set the theme of the Activity that directly follows your splash screen. -->
   <!-- Required -->
   <item name="postSplashScreenTheme">@style/Theme.App</item>
</style>

Set it up in your manifest:

<manifest>
   <application android:theme="@style/Theme.App.Starting">
    <!-- or -->
        <activity android:theme="@style/Theme.App.Starting">
...
Font answered 6/10, 2022 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.