I have an issue with my SplashScreenActivity, when I start my application on my phone it shows a white screen for about 0,5 seconds. The MainActitivy extends FragmentActivity and in the AndroidManifest I declare the SplashScreenActivity as launcher and portrait mode as screenOrientation.
The code:
public class SplashScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
randomSplash();
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
}
private void randomSplash(){
Random random = new Random();
int i = random.nextInt(4);
LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);
switch(i){
case 1:
ln.setBackgroundResource(R.drawable.splash1);
break;
case 2:
ln.setBackgroundResource(R.drawable.splash2);
break;
case 3:
ln.setBackgroundResource(R.drawable.splash3);
break;
default:
ln.setBackgroundResource(R.drawable.splash0);
break;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
this is your problem UI thread to sleep not a very good idea use handler instead
and I think it may cause an exception too.
Handler h=new Handler();
h.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
finish();
}
}, 2000);
}
You need to run this two actions in an AsyncTask:
setContentView(R.layout.splashscreen);
randomSplash();
put the setContentView in the doInBackground-method and in the postExecute method you run randomSplash.
Change SplashActivity theme in the AndroidManifest.xml file to this.
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Related
I've created a splash screen, and it works pretty fine at first, but after that, it shows me a white blank screen instead of my splash screen image file. I've no idea why that happens.
I tried to change my style.xml parent theme, but some of the themes crash my app, and only Theme.AppCompat.Light.NoActionBar works and gives me a blank white screen.
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Splash.java
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread ssThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
startActivity(startMainScreen);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ssThread.start();
}
}
Screen sequence, thread sleep time, and everything else works fine except that the image is not showing.
In your onCreate method, you forgot to add setContentView(R.layout.splash);
You need to add setContentView to your onCreate method.
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
*add setContentView here after super.onCreate( )
*/
setContentView( R.layout.splash_layout);
Thread ssThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
startActivity(startMainScreen);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ssThread.start();
}
}
YOU MISSING setContentView(R.layout.YOUR_XML_NAME);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxx);
/****** Create Thread that will sleep for 3 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 5 seconds redirect to another intent
Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
startActivity(startMainScreen);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
instead of using thread and sleep function use Handler , and do some thing like this :
setContentView(R.layout.splash_screen);
int interval = 3000; // 3 second
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
}, interval);
You have to set Layout in onCreate method of Splash activity like:
setContentView(R.layout.splash);
This question already has answers here:
How do I make a splash screen? [closed]
(31 answers)
Closed 8 years ago.
So, I just read this question: How do I make a splash screen?
But instead of adding a fixed delay (like in the top answer), I wanted to keep the splash screen on while the MainActivity (with a MapFragment) loads.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
synchronized (this) {
try {
wait(3000);
System.out.println("Thread waited for 3 seconds");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
try {
t.start();
t.join();
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I added the wait(3000) line, because I noticed before that the thread didn't live for long. However, if I make it wait for longer, there's just a black screen that lasts longer. For some reason, the SplashScreen activity won't show the ImageView.
What should I do?
Thanks.
Easy way to do it..
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.tabs.R;
public class Splash extends Activity implements Runnable
{
Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mThread = new Thread(this);
mThread.start();
}
#Override
public void run()
{
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}
}
splash.xml if you want to show a image
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
Note if you want to do some UI operation in splash .Then you have to create a handler and update UI in it.
The main thread cannot be blocked for a long time. You should use Handler to trigger another event if you want to start another event in 3 seconds. You can use sendMessageDelayed. In addition, startActivity should be called in main thread.
make a splash screen like this:
while(counter < 1300) {
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
counter+=100;
}
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
Hope it helps!
EDIT:
Anotehr way to make a splash would be like this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
startActivity(intent);
}
},3000); -> the splash will show for 3000 ms, you can reduce this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am coding in Eclipse for an Android App. I have developed a splash screen which I need to display for 5 seconds before my app starts. How to do it?
Thread timer=new Thread()
{
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
Intent i=new Intent(SplashScreen.this,MainActivity.class);
finish();
startActivity(i);
}
}
};
timer.start();
Use the Async Class to perform the sleep operation in the doinbackground function and in the post function do the rest of the task
public class SplashScreenActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
#Override
public void onCreate(Bundle icicle)
{ super.onCreate(icicle);
try{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splashscreen);
}catch(Exception e){
e.printStackTrace();
}
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
}
#Override
protected Void doInBackground(Void... voids){
try {
Thread.sleep(SPLASH_DISPLAY_LENGHT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void params)
{
startActivity(new Intent(SplashScreenActivity.this, HomeActivity.class));
finish();
}
}
}
use like that
public class SplaceScreenActivity extends Activity {
private static final int SPLASH_DISPLAY_TIME = 2500;
// SplashScreen Splash;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splacescreen);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent();
intent.setClass(SplaceScreenActivity.this,
HomeScreenActivity.class);
SplaceScreenActivity.this.startActivity(intent);
SplaceScreenActivity.this.finish();
}
}, SPLASH_DISPLAY_TIME);
}
}
Try below code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final int welcomeScreenDisplay = 2000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
} finally {
startActivity(new Intent(MainActivity.this,
HomeActivity.class));
finish();
}
}
};
welcomeThread.start();
}
}
Handler handler = new Handler();
Runnable run = new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplaceActivity.this, New.class));
overridePendingTransition(0, 0);
finish();
}
};
handler.postDelayed(run, 3000);
Use AsyncTask or thread for this purpose.
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
hope it helps
I use Timer:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
Intent home_page = new Intent(Splash.class,HomePage.class);
startActivity(home_page);
finish();
}}, 5000);
you can use Sleep method like this in your Splash Activity onCreate method:
Thread timer1 = new Thread(){
#Override
public void run(){
try{
sleep(4000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashActivity.this, NextActivity.class);
startActivity(intent);
}
}
};
timer1.start();
this take 4 sec to load NextActivity.
Add these few lines of code in your splashActivity and it will start your second activity after 5seconds.
new Handler().postDelayed(new Runnable(){
public void run() {
Intent mainIntent = new Intent(splashScreen.this,MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
splashScreen.this.startActivity(mainIntent);
splashScreen.this.finish();
}
}, 5000);
PostDelayed Causes the Runnable to be added to the message queue, to
be run after the specified amount of time elapses. The runnable will
be run on the thread to which this handler is attached.
Use handler
public class SplashActivity extends Activity {
int secondsDelayed = 5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg) {
switch(msg.what)
{
case 1:
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
break;
case 0:
{
Message ms = new Message();
ms.what = 1;
mHandler.sendMessageDelayed(ms, secondsDelayed * 1000);
}
break;
}
};
};
protected void onDestroy() {
super.onDestroy();
mHandler.removeMessages(1);
};
}
Note:Donot make Splash screen for 5 sec user will get irritated make it for 2sec
I want to show a "boot logo" or image before of setting the final layout of the main activity. Actually i do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authors);
mContext = this;
showAuthors();
where showAuthors run this:
private void showAuthors()
{
setContentView(R.layout.authors);
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
};
logoTimer.start();
try {
logoTimer.join();
setContentView(R.layout.activity_main);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
the file authors.xml ( R.layout.authors ) is the same of activity_main, but clean, containing just a pair of strings with my name and email:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#raw/call2"
android:src="#raw/call2"
android:scaleType = "centerCrop"
tools:context=".MainActivity" >
<TextView
android:id="#+id/authorsTV"
android:textColor="#FF6600"
android:textSize="16.5sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="33dp"
android:text="#string/serviceStatus" />
</RelativeLayout>
the problem is: all works, but the screen is all white as an empty layout.
where i'm doing wrong? thank you all if consider to reply me!
You should not call thread.join from the main thread, because you can get an ANR crash.
Here's how to do it with minimal changes to your existing code. Remove everything after logoTimer.start(), and put this inside the try block, immediately after sleep(3000):
runOnUiThread(new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
});
But it would be cleaner to rewrite that whole method like this:
private void showAuthors()
{
setContentView(R.layout.authors);
new Handler().postDelayed( new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
}, 3000);
}
First you have two ways of showing a splash screen,
1.with activity
public class SplashActivity extends Activity implements Runnable
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
setContentView(R.layout.act_spalsh);
hideSplashActivity();
}
private void hideSplashActivity()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
startActivity(new Intent(this, MainActivity .class));
finish();
}
2.with dialog
public class MainActivity extends Activity implements Runnable
{
Dialog splashDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
splashDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
splashDialog.setContentView(R.layout.dlg_splash);
splashDialog.show();
hideSplashDialog();
setContentView(R.layout.act_main);
}
private void hideSplashDialog()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
splashDialog.dismiss();
splashDialog = null;
}
I prefer using dialog unless you have some trouble with that
I'm getting error with this code. Why huhu
123123123
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(1500);
splash.setImgeResource(R.drawable.dilclogo);
sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(MainActivity.this, MenuScreen.class);
startActivity(intent);
}
}
};
timer.start();
This is because you can NOT access your UI/Main thread directly from any other thread. You can use below methods to access your UI thread though:
Using AsyncTask
Using runOnUiThread()
You can also read this article on threading in android to help you understand this concept better.
put splash.setImgeResource(R.drawable.dilclogo); line into runOnUiThread .
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
splash.setImageResource(R.drawable.billboard_image);
}
});
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
splash.setImageResource(R.drawable.square);
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
System.out.println("finally");
}
}
};
timer.start();
You should update ui on the ui thread. Use runonUithread.
runOnUiThread(new Runnable() {
#Override
public void run() {
// set image to imageview here
// ui should be updated on the ui thread.
// you cannot update ui from a background thread
}
});
But i would suggest you to use a handler.
public class Splash extends Activity {
//stopping splash screen starting home activity.
private static final int STOPSPLASH = 0;
//time duration in millisecond for which your splash screen should visible to
//user. here i have taken half second
private static final long SPLASHTIME = 500;
//handler for splash screen
private Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//Generating and Starting new intent on splash time out
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
Splash.this.finish();
break;
}
super.handleMessage(msg);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//Generating message and sending it to splash handle
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
splash.xml
<?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:orientation="vertical" android:background="#drawable/mydrawable">
// have a imageview and set background to imageview
</RelativeLayout>
Using handlers and postdelayed
public class Splash extends Activity {
private static final int SPLASH_TIME = 2 * 1000;// 3 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ImageView iv= (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.afor);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
Splash.this.finish();
}
}, SPLASH_TIME);
} catch(Exception e)
{
e.printStacktrace();
}
}
#Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
splash.xml
<?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:orientation="vertical" android:background="#ffffaa">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
You can not use normal threading on android system.
Give you some example on thread on android :D
---> Android Asynctask
Android Developer - Android Asynctask
You can use this for some loading effect on UI in android.
---> runOnUiThread
In your case, I suggest to use this.
You can have more detail here.
Click for detail
USEAGE::
runOnUiThread(new Runnable() {
#Override
public void run() {
// Do you ui update here
}
});
public class vv extends Activity {
int b[] = {R.drawable.a, R.drawable.m, R.drawable.b, R.drawable.j, R.drawable.er, R.drawable.chan, R.drawable.vv};
public ImageView i;
int z = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = (ImageView) findViewById(R.id.image);
i.setImageResource(b[0]);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
for (z = 0; z < b.length + 2; z++) {
if (z < b.length) {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
i.setImageResource(b[z]);
}
});
} else {
z = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
}
};
timer.start();
}
}
Perhaps consider using
AsyncTask.execute(new Runnable {
public void run() {
splash.setImageResource(R.drawable.square);
}
});