Reputation: 31
I am trying my application on my galaxy s5 and s6 but on this devices my application not show the splash screen(background from ImageView) (the app show white screen and go to the main). I try on a virtual device on the android studio and it work with out problem. can anyone know why this is happened and how can I fix that ?
My SplashScreen Class -
protected void onCreate(Bundle savedInstanceState) {
// hide action bar from the activity
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
/****** Create Thread that will sleep for 6 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 6 seconds
sleep(6 * 1000);
// After 5 seconds redirect to another intent
Intent i = new Intent(getBaseContext(), MainActivity.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
Thanks !
Upvotes: 2
Views: 3335
Reputation: 150
here it is the google ui pattern for Launch Screens, the problem is that you are implementing it the incorrectly way, making using of a delay, the user should not wait the amount of time the takes to launch the "app" + the delay, I made this error before as well, the splash view that you see has to be ready immediately, before you show a layout file in your splash activity, so here is the correct way to implement it:
1 - On Res/Drawable create a xml, that will be your layout file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/common_signin_btn_dark_text_default"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
2 - Navigate to Res/Values and edit styles.xml adding your layout.
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splashscreen</item>
</style>
3 - On your AndroidManifest.xml file set your SplashScreen Activity to use the theme created.
<activity
android:name=".SplashScreen"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4 - And finally in your SplashScreen Activity just forward it to your main Activity.
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
} // SplashScreen class end
That should just take the normal amount of time spent to launch the "app" and no delays implementation in between.
Upvotes: 0
Reputation: 541
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
getSupportActionBar().hide();
/****** Create Thread that will sleep for 6 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 6 seconds
sleep(6 * 1000);
// After 5 seconds redirect to another intent
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
try this :)
Hide Action Bar After setContentView
Upvotes: 0
Reputation: 625
You have 2 options depending on what you want to do.
Option 1
You want to just show a splash screen and move on. No heavy data loading/preparing is occurring in the background.
In this case make a SplashActivity, that has your image either as a background or inside an ImageView. Inside SplashActivity's onCreate()
do this:
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
startActivity(SplashActivity.this, MainActivity.class);
}, SPLASH_TIMEOUT);
Where SPLASH_TIMEOUT is the time in milliseconds you want to display the splash screen.
Option 2
Show splash screen while data is being loaded in the background.
In this case, put a ImageView
on top of the view hierarchy and make its width and height match_parent
. Give it some id to be referenced from the code.
After you are done loading/fetching data you probably got some callbacks that inform you that everything is done, and just in that callback do
splashImage.setVisibility(View.GONE);
and thats it!
Upvotes: 0