I want my app to look more professional, so I decided to add a splash screen.
How should I go about the implementation?
I want my app to look more professional, so I decided to add a splash screen.
How should I go about the implementation?
Further reading:
Old answer:
HOW TO: Simple splash screen
This answers shows you how to display a splash screen for a fixed amount of time when your app starts for e.g. branding reasons. E.g. you might choose to show the splash screen for 3 seconds. However if you want to show the spash screen for a variable amount of time (e.g. app startup time) you should check out Abdullah's answer https://mcmap.net/q/64074/-how-do-i-make-a-splash-screen-closed. However be aware that app startup might be very fast on new devices so the user will just see a flash which is bad UX.
First you need to define the spash screen in your layout.xml
file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>
</LinearLayout>
And your activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Thats all ;)
SPLASH_DISPLAY_LENGTH
time. You should do this instead: bignerdranch.com/blog/splash-screens-the-right-way –
Dillingham Note this solution will not let the user wait more: the delay of the splash screen depends on the start up time of the application.
When you open any android app you will get by default a some what black screen with the title and icon of the app on top, you can change that by using a style/theme.
First, create a style.xml in values folder and add a style to it.
<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
Instead of using @android:style/Theme.DeviceDefault.Light.NoActionBar
you can use any other theme as a parent.
Second, in your app Manifest.xml add android:theme="@style/splashScreenTheme"
to your main activity.
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:theme="@style/splashScreenTheme" >
Third, Update your theme in your onCreate() launch activity.
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.mainAppTheme);
super.onCreate(savedInstanceState);
}
UPDATE Check out this post.
Thanks to @mat1h and @adelriosantiago
<item name="android:background">
would override the windowBackground
. And without android:background
defined, my background in any fragments would be transparent revealing the activity behind the foreground content. –
Val splashScreenTheme
–
Addams android:opacity="opaque"
, so the full line in launch_background.xml
looks like: <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
–
Mouthful android:background
as this also affects the background of other views, not just to window background. In my case i had to display an AlertDialog
when some dependencies were missing, and every component of the dialog had that background –
Trollope Further reading:
Old answer:
HOW TO: Simple splash screen
This answers shows you how to display a splash screen for a fixed amount of time when your app starts for e.g. branding reasons. E.g. you might choose to show the splash screen for 3 seconds. However if you want to show the spash screen for a variable amount of time (e.g. app startup time) you should check out Abdullah's answer https://mcmap.net/q/64074/-how-do-i-make-a-splash-screen-closed. However be aware that app startup might be very fast on new devices so the user will just see a flash which is bad UX.
First you need to define the spash screen in your layout.xml
file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>
</LinearLayout>
And your activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Thats all ;)
SPLASH_DISPLAY_LENGTH
time. You should do this instead: bignerdranch.com/blog/splash-screens-the-right-way –
Dillingham your Splash.java may look like this:
public class Splash extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, ActivityB.class));
finish();
}
}, secondsDelayed * 1000);
}
}
change ActivityB.class
to whichever activity you want to start after the splash screen
check your manifest file and it should look like
<activity android:name=".HomeScreen" android:label="@string/app_name"> </activity> <activity android:name=".Splash" android:label="@string/title_activity_splash_screen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
The answers above is very good, but I would like to add something else. I am new to Android, I met these problem during my development. hope this can help someone like me.
The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml.
<activity
android:name=".SplashActivity"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time.
public class SplashActivity extends Activity {
private static boolean splashLoaded = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!splashLoaded) {
setContentView(R.layout.activity_splash);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
}
else {
Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
happy coding!
android:noHistory="true"
in AndroidManifest.xml
to prevent the user from going back to the splash screen using the back button. –
Nahamas Abdullah's answer is great. But i want to add some more details to it with my answer.
Implementing a Splash Screen
Implementing a splash screen the right way is a little different than you might imagine. The splash view that you see has to be ready immediately, even before you can inflate a layout file in your splash activity.
So you will not use a layout file. Instead, specify your splash screen’s background as the activity’s theme background. To do this, first create an XML drawable in res/drawable.
background_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background Color -->
<item
android:drawable="@color/gray"/>
<!-- Logo -->
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
It just a layerlist with logo in center background color with it.
Now open styles.xml and add this style
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
This theme will have no actionbar and with background that we just created above.
And in manifest you need to set SplashTheme to activity that you want to use as splash.
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Then inside your activity code navigate user to the specific screen after splash using intent.
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
That's the right way to do. I used these references for answer.
YouTube
regarding about this. But I think the bitmap size will be the problem since you cannot resize it using layer-list
. –
Carabin Implementing a Splash Screen
section is very useful, thanks! –
Groark <bitmap>
tag anymore this can raise ResourceNotFound exception; instead use android:drawable
instead of android:src
in the <itme>
directly –
Presley windowBackground
? or in related layer-list? –
Bureaucratize Create an Activity
SplashScreen.java
public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in ms
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (Exception e) {
} finally {
startActivity(new Intent(SplashScreen.this,
MainActivity.class));
finish();
}
};
};
splashTread.start();
}
}
splashscreen.xml
will be like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600px" android:layout_height="1024px"
android:background="#FF0000">
</RelativeLayout>
A Splash Screnn, by default, does not automatically make your Application look more professional. A professionally designed Splash Screen has a possibility of making your Application look more professional, but if you do not know how to write one then how professional will the rest of your Application actually be.
About the only reason (excuse) to have a Splash Screen is because you are doing a massive amount of Calculations or are waiting for GPS/WiFi to startup because your Application relies on that prior to it starting. Without the result of those Calculations or access to GPS/WiFi (etc.) your Application is dead in the water, thus you feel you need a Splash Screen, and MUST block the view of the Screen for any other running Programs (including the Background).
Such a Splash Screen ought to look like your Full Screen Application to give the impression that it has already initialized, then after the lengthy calculations are completed the final details could be filled in (the Image tweaked). The chance of that being the case or that it is the only way the Program could be designed is mighty small.
It would be better to allow the User (and the rest of the OS) to do something else while they wait rather than design your Program to be dependant on something that will take a while (when the duration of the wait is uncertain).
There are Icons on your Phone already that say that GPS/WiFi is starting. The time or space taken up by the Splash Screen could be spent loading pre-calculations or actually doing the Calculations. See the first Link below for the problems you create and what must be considered.
If you absolutely must wait for these Calculations or GPS/WiFi it would be best to simply let the Application start and have a pop-up that says that it is necessary to wait for the Calculations (a TEXTUAL "Initializing" Message is fine). The wait for GPS/WiFi is expected (if they were not enabled in another Program already) so announcing their wait times are unnecessary.
Remember that when the Splash Screen starts your Program IS actually running already, all you are doing is delaying the use of your Program and hogging the CPU/GPU to do something that most do not feel is necessary.
We had better really want to wait and see your Splash Screen every time we start your Program or WE will not feel it is very professionally written. Making the Splash Screen FULL Screen and a duplicate of the actual Program's Screen (so we think it is initialized when in fact it has not) MIGHT accomplish your goal (of making your Program look more professional) but I would not bet much on that.
Why not to do it: http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/
How to do it: https://encrypted.google.com/search?q=Android+splash+screen+source
So there is a good reason not to do it but IF you are certain that somehow your situation falls outside those examples then the means to do it is given above. Be certain that it really does make your Application look more professional or you have defeated the only reason you gave for doing this.
It is like a YouTube Channel that starts every Video with a lengthy Graphic Intro (and Outro) or feels the need to tell a Joke or explain what happened during the past week (when it is not a Comedy or LifeStyles Channel). Just show the show ! (Just run the Program).
Above all answers are really very good. But there are encounter problem of memory leakage. This issue is often known in the Android community as "Leaking an Activity". Now what exactly does that mean?
When configuration change occurs, such as orientation change, Android destroys the Activity and recreates it. Normally, the Garbage Collector will just clear the allocated memory of the old Activity instance and we're all good.
"Leaking an Activity" refers to the situation where the Garbage Collector cannot clear the allocated memory of the old Activity instance since it's being (strong) referenced
from an object that out lived the Activity instance. Every Android app has a specific amount of memory allocated for it. When Garbage Collector cannot free up unused memory, the app's performance will decrease gradually and eventually crash with OutOfMemory
error.
How to determine whether the app leaks memory or not? The fastest way is to open the Memory tab in Android Studio and pay attention to allocated memory as you change the orientation. If the allocated memory keeps on increasing and never decreases then you have a memory leak.
1.Memory leak when user change the orientation.
First you need to define the splash screen in your layout resource splashscreen.xml
file
Sample Code for splash screen activity.
public class Splash extends Activity {
// 1. Create a static nested class that extends Runnable to start the main Activity
private static class StartMainActivityRunnable implements Runnable {
// 2. Make sure we keep the source Activity as a WeakReference (more on that later)
private WeakReference mActivity;
private StartMainActivityRunnable(Activity activity) {
mActivity = new WeakReference(activity);
}
@Override
public void run() {
// 3. Check that the reference is valid and execute the code
if (mActivity.get() != null) {
Activity activity = mActivity.get();
Intent mainIntent = new Intent(activity, MainActivity.class);
activity.startActivity(mainIntent);
activity.finish();
}
}
}
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
// 4. Declare the Handler as a member variable
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
// 5. Pass a new instance of StartMainActivityRunnable with reference to 'this'.
mHandler.postDelayed(new StartMainActivityRunnable(this), SPLASH_DISPLAY_LENGTH);
}
// 6. Override onDestroy()
@Override
public void onDestroy() {
// 7. Remove any delayed Runnable(s) and prevent them from executing.
mHandler.removeCallbacksAndMessages(null);
// 8. Eagerly clear mHandler allocated memory
mHandler = null;
}
}
For more information please go through this link
This is the full code here
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_DURATION = 1000;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_DURATION);
}}
In drawables create this bg_splash.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/app_color"/>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_in_app_logo_big"/>
</item></layer-list>
In styles.xml create a custom theme
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/bg_splash</item>
</style>
and finally in AndroidManifest.xml specify the theme to your activity
<activity
android:name=".activities.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Cheers.
drawable
–
Elaterin Splash screens should not be loaded from a layout file, there might still be some lag when loading it.
The best way is to create a Theme just for your SplashScreenActivity and set the android:windowBackground
to a drawable ressource.
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
In a nutshell:
Declare your SplashScreenActivity in the manifest:
<activity
android:name=".activities.SplashScreenActivity"
android:theme="@style/SplashTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your SplashScreenActivity.java:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity_.class);
startActivity(intent);
finish();
}
Next create the ressource for the background window of your theme:
<style name="SplashTheme" parent="Theme.Bumpfie.Base">
<item name="android:windowBackground">@drawable/splash</item>
</style>
Drawable file splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white"/>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/app_logo"/>
</item>
</layer-list>
After Android Marshmallow, other Productive use of Splash screen I come to think of is requesting necessary Android Permissions
in your app's splash screen.
it seems like most apps handle permission request this way.
Dialogs make bad UIX and they break the main flow and make you decide on runtime and truth is most users might not even care if your app want to write something on SD card. Some of them might not even understand what we are trying to convey until we translate it in plain english.
Requesting permissions at one time make less number of "if else" before every operation and make your code looks clutter free.
This is a example of how you can ask for permissions in your splash activity for device running Android OS 23+ .
If all permissions are granted OR already granted OR app is running on Pre Marshmallow THEN just go and display main contents with little delay of half second so that user can appreciate effort we had put in reading this question and trying to give our best.
import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.c2h5oh.beer.R;
import com.c2h5oh.beer.utils.Animatrix;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SplashActivity extends AppCompatActivity {
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//show animations
Animatrix.scale(findViewById(R.id.title_play), 100);
Animatrix.scale(findViewById(R.id.title_edit), 100);
Animatrix.scale(findViewById(R.id.title_record), 100);
Animatrix.scale(findViewById(R.id.title_share), 100);
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+ Permission APIs
fuckMarshMallow();
} else {
// Pre-Marshmallow
///Display main contents
displaySplashScreen();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.MODIFY_AUDIO_SETTINGS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.VIBRATE, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
// Permission Denied
Toast.makeText(SplashActivity.this, "All Permission GRANTED !! Thank You :)", Toast.LENGTH_SHORT)
.show();
displaySplashScreen();
} else {
// Permission Denied
Toast.makeText(SplashActivity.this, "One or More Permissions are DENIED Exiting App :(", Toast.LENGTH_SHORT)
.show();
finish();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@TargetApi(Build.VERSION_CODES.M)
private void fuckMarshMallow() {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("Read SD Card");
if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
permissionsNeeded.add("Record Audio");
if (!addPermission(permissionsList, Manifest.permission.MODIFY_AUDIO_SETTINGS))
permissionsNeeded.add("Equilizer");
if (!addPermission(permissionsList, Manifest.permission.VIBRATE))
permissionsNeeded.add("Vibrate");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "App need access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
Toast.makeText(SplashActivity.this, "No new Permission Required- Launching App .You are Awesome!!", Toast.LENGTH_SHORT)
.show();
displaySplashScreen();
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@TargetApi(Build.VERSION_CODES.M)
private boolean addPermission(List<String> permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
/**
* Display main content with little delay just so that user can see
* efforts I put to make this page
*/
private void displaySplashScreen() {
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, AudioPlayerActivity.class));
finish();
}
}, 500);
}
}
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t=new Thread()
{
public void run()
{
try {
sleep(2000);
finish();
Intent cv=new Intent(MainActivity.this,HomeScreen.class/*otherclass*/);
startActivity(cv);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
Create a Activity, let us Activity named 'A', then create a xml file called myscreen.xml, in that set a the splash screen image as background, and then use count down timer to navigate from one Activtity to another. To know how to use Count Down timer see my answer in this question TimerTask in Android?
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);
}
}
Splash screen is a little unusable object in Android: it can not be loaded as soon as possible for hiding the delay of main activity starting. There are two reasons to use it: advertising and network operations.
Implementation as dialog makes jump without delay from splash screen to main UI of activity.
public class SplashDialog extends Dialog {
ImageView splashscreen;
SplashLoader loader;
int splashTime = 4000;
public SplashDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
setCancelable(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
cancel();
}
}, splashTime);
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<ImageView
android:id="@+id/splashscreen"
android:layout_width="190dp"
android:layout_height="190dp"
android:background="@drawable/whistle"
android:layout_centerInParent="true" />
</RelativeLayout>
And start:
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getCategories() != null && getIntent().getCategories().contains("android.intent.category.LAUNCHER")) {
showSplashScreen();
}
}
protected Dialog splashDialog;
protected void showSplashScreen() {
splashDialog = new SplashDialog(this, R.style.SplashScreen);
splashDialog.show();
}
...
}
Another approach is achieved by using CountDownTimer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new CountDownTimer(5000, 1000) { //5 seconds
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}.start();
}
Sometime user open the SplashActivity
and quit immediately but the app still go to MainActivity
after SPLASH_SCREEN_DISPLAY_LENGTH
.
For prevent it: In SplashActivity
you should check the SplashActivity
is finishing or not before move to MainActivity
public class SplashActivity extends Activity {
private final int SPLASH_SCREEN_DISPLAY_LENGTH = 2000;
@Override
public void onCreate(Bundle icicle) {
...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {//isFinishing(): If the activity is finishing, returns true; else returns false.
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, SPLASH_SCREEN_DISPLAY_LENGTH);
}
}
}
Hope this help
- Add in SplashActivity
public class SplashActivity extends Activity {
private ProgressBar progressBar;
int i=0;
Context context;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
context = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, LoginActivity.class));
finish();
}
}, 2000);
}
}
- Add in activity_splash.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash">
<ImageView
android:id="@+id/ivLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/icon_splash"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<ProgressBar
android:id="@+id/circle_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:max="100"
android:progressTint="@color/green"
android:visibility="visible" />
</RelativeLayout>
- Add in AndroidManifest.xml
<activity android:name="ex.com.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Really easy & gr8 approach :
First create your splash with the following website :
https://www.norio.be/android-feature-graphic-generator/
Choose your logo and slogan, choose your beautiful background. resize it to 4096x4096.
Now download that picture and uplodate it to :
https://apetools.webprofusion.com/app/#/tools/imagegorilla
And generate all the splash screens needed, all devices, all platforms.
Enjoy!
There are enough answers here that will help with the implementation. this post was meant to help others with the first step of creating the splash screen!
In Kotlin write this code:-
Handler().postDelayed({
val mainIntent = Intent(this@SplashActivity, LoginActivity::class.java)
startActivity(mainIntent)
finish()
}, 500)
Hope this will help you.Thanks........
How about a super-flexible launch screen that can use the same code and is defined in the AndroidManifest.xml, so the code will never need to change. I generally develop libraries of code and do not like customizing code because it is sloppy.
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="launch_class" android:value="com.mypackage.MyFirstActivity" />
<meta-data android:name="duration" android:value="5000" />
</activity>
Then the SpashActivity itself looks up the meta-data for "launch_class" to then make the Intent itself. The meta data "duration" defines how long the splash screen stays up.
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);
ComponentName componentName = new ComponentName(this, this.getClass());
try {
Bundle bundle = null;
bundle = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA).metaData;
String launch_class = bundle.getString("launch_class");
//default of 2 seconds, otherwise defined in manifest
int duration = bundle.getInt("duration", 2000);
if(launch_class != null) {
try {
final Class<?> c = Class.forName(launch_class);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, c);
startActivity(intent);
finish();
}
}, duration);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
Though there are good answers, I will show the google recommended way:
1)First create a Theme
for splash screen:
you have a theme called splashscreenTheme
, your launcher theme would be:
<style name="splashscreenTheme">
<item name="android:windowBackground">@drawable/launch_screen</item>
</style>
Note:
android:windowBackground
already sets your splashscreen image no
need to do this in UI again.
you can also use color here instead of a drawable.
2)Set the theme to manifest of splashscreenActivity
<activity
android:name=".activity.splashscreenActivity"
android:screenOrientation="portrait"
android:theme="@style/splashscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3)make sure you launch_screen drawable
is not in drawable
folder if your image is not small.
It will result in faster launch screen start and save you from the black screen
It also avoids extra overdraw
This is the best post I've seen on splash screens: http://saulmm.github.io/avoding-android-cold-starts
Saúl Molinero goes into two different options for splash screens: Taking advantage of the window background to animate into your initial screen and displaying placeholder UI (which is a popular choice that Google uses for most of their apps these days).
I refer to this post every time I need to consider cold start time and avoiding user dropoff due to long startup times.
Hope this helps!
In my case I didn't want to create a new Activity only to show a image for 2 seconds. When starting my MainAvtivity
, images gets loaded into holders using picasso, I know that this takes about 1 second to load so I decided to do the following inside my MainActivity OnCreate
:
splashImage = (ImageView) findViewById(R.id.spllll);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
splashImage.setVisibility(View.GONE);
}
}, secondsDelayed * 2000);
When starting the application the first thing that happens is the ImageView
gets displayed and the statusBar is removed by setting the window flags to full screen. Then I used a Handler
to run for 2 seconds, after the 2 seconds I clear the full screen flags and set the visibility of the ImageView
to GONE
. Easy, simple, effective.
Its really simple in android we just use handler concept to implement the splash screen
In your SplashScreenActivity java file paste this code.
In your SplashScreenActivity xml file put any picture using imageview.
public void LoadScreen() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
startActivity(i);
}
}, 2000);
}
You can add this in your onCreate Method
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// going to next activity
Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(i);
finish();
}
},time);
And initialize your time value in milliseconds as yo want...
private static int time=5000;
for more detail download full code from this link...
Here is a simple one!
~Lunox
MainActivity.java
package com.example.splashscreen;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
splashscreen.java
package com.example.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class splashscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//Splash Screen duration
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(splashscreen.this, MainActivity.class));
finish();
}
}, secondsDelayed * 3000);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splashlogo"
/>
splashlogo.png
GitHub
One way is by creating a FullScreenActivity/EmptyActivity (say SplashScreenActivity) and set it as the first Activity that shows up when you open your app. Add the following to your Activity in the AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You can then set a handler to dismiss this activity after a few seconds.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i= new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(i); //start new activity
finish();
}
}, 3000); //time in milliseconds
Secondly, if you don't wish to create a separate activity, you can inflate a layout over your MainActivity
and set the layout visibility to GONE
or inflate the main layout over the existing splash screen layout after a few milliseconds.
Simple Code, it works:) Simple splash
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(LoginSuccessFull.this, LoginActivity.class));
finish();
}
}, secondsDelayed * 1500);
public class SplashActivity extends Activity {
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_splash);
Thread thread = new Thread(){
public void run(){
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent in = new Intent(ctx,MainActivity.class);
startActivity(in);
finish();
}
};
thread.start();
}
}
I used threads to make the Flash Screen in android.
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class HomeScreen extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_home);
Thread thread = new Thread(){
public void run(){
try {
Thread.sleep(3 * 1000);
Intent i = new Intent(HomeScreen.this, MainActivity.class);
startActivity(i);
} catch (InterruptedException e) {
}
}
};
thread.start();
}
}
© 2022 - 2024 — McMap. All rights reserved.