During loading of my android application i want to show a logo during the first 2,3 seconds of launching, I think it's less ugly than just seeing a UI after launching.
So in my code i make first a setContentView with the logo (splash) and after a setContentView with the UI (main).
The pb is instead of seeing the logo screen I just see a black screen.
I am doing that in main thread so I don't understand.
Do you have an explanation of the problem and if possible a workaround ?
The not working well onCreate() of the activity is the following (black screen during 5 seconds and after can see the UI, no exception thrown) :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); //doesn't work
Log.i(TAG, Thread.currentThread().getName());
//this.r
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.i(TAG, Thread.currentThread().getName() + e.toString() );
}
setContentView(R.layout.main); //work OK
}
}
if I just left the class with the code below there is no problem I can see the "first" and only view :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); //work OK
}
}
You can use TimerTask instead of using Thread.sleep for making wait in main UI thread as:
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// use runOnUiThread for Updating Ui elements
Your_Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// set Activity Next layout Here
setContentView(R.layout.main);
}
});
}
}, 5000);
The pb is instead of seeing the logo screen I just see a black screen.
Thread.sleep() is the wrong approach. You are preventing everything from happening including showing your splash screen.
The not working well onCreate() of the activity is the following (black screen during 5 seconds and after can see the UI, no exception thrown)
The layout is only displayed after onResume() is called, so by blocking the UI Thread in onCreate() you have created the blank screen. Then when the Thread resumes both layouts are drawn, but you'll never see the splash screen since the next setContentView() is called a few milliseconds later.
While the idea of using a splash screen is debatable, you should use a callback to change layouts after five seconds. Use a Handler and Runnable to change screens.
To insert my personal opinion, I would reject any app that takes 5 seconds to load. I have better things to do than look at a logo even for 2-3 seconds every time I open the app.
While I like ρяσѕρєя K's answer, I'll play devil's advocate. I prefer to use the root View's built-in handler:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
findViewById(android.R.id.content).postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.main);
}
}, 1000);
}
Related
I have the Problem that my Android app does not delay a second (or 10 seconds), if I use the postDelayed method..
Basically I would like my program to wait one second after I clicked the button, then update the text on my textview ("READY"), wait another 2 seconds, then update the textview again ("SET") and then it should start another activity (not yet implemented :-) ).
With my code, the programm starts and after I click the button the textview shows the last text ("SET") immediately.. It just does not wait.
What am i doing wrong?
Here is my code:
public class MyCounterActivity extends Activity {
private long mInternval = 100000;
private Handler mHandler;
private Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
//updateInterval(); //change interval
startRepeatingTask();
}
};
void startRepeatingTask(){
mHandler.postDelayed(mStatusChecker, mInternval);
//mStatusChecker.run();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gym_counter);
final TextView tv1 = (TextView) findViewById(R.id.fullscreen_content);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final long up;
EditText textUp = (EditText) findViewById(R.id.editTextUp);
up = Integer.parseInt(textUp.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
},1000);
Log.d("after 1 runnable", "whaaat");
tv1.setText("Ready");
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
}, 2000);
Log.d("after 2nd runnable", "whaaat 2");
//startRepeatingTask();
tv1.setText("SET");
}
});
}
I also tried to run it with the runOnUiThread() (within the onClick(View v) but with with the same result). I expected it to wait 1 second (startRepeatingTask()) and then runs the loop and waits several seconds...
runOnUiThread(new Runnable() {
#Override
public void run() {
startRepeatingTask();
for (int u = 0; u < up; u++){
startRepeatingTask();
}
}
}
});
Hope my description makes sense :-).
Thank you for your help!
EDIT:
I was now able to find a solution for my first problem. The answer from #mad in this post helpded me: How to start a different activity with some delay after pressing a button in android?
(Thats probably the same thing that #laalto tried to tell me. Thanks for the hint!)
In the onClick()
tv1.setText("READY");
mHandler.postDelayed(mDelay1, 2000);
And then the Runnable
private Runnable mDelay1 = new Runnable() {
#Override
public void run() {
if (tv1.getText()=="READY")
tv1.setText("SET");
}
};
BUT:
If i want to refresh the text on my Textview after every second, how do i do that? I cant just call mHandler.postDelayed() several times.. Any help is appreciated.
When you call postDelayed(), it just places the Runnable in a queue and returns immediately. It does not wait for the runnable to be executed.
If you need something to happen after a delay, put the code in the run() method of the runnable.
Whenever you call something like Thread.start(), handler.postDelayed, view.postDelayed, AsynchTask, TimerTask .. you enter the world of threading or you might call it parallel computing.
So there can be multiple threads ("codes") running at the same time.
When you are inside your Activity it is running in a Thread that is calld UI-thread or main thread. All graphics is handled in that thread and that thread alone.
Do NEVER wait in the UI-thread!
Example: you have a button that switches color from say gray to yellow on pressing it. Now you enter a Thread.sleep(10000); - waiting 10 seconds at the start of your onClick.
You will then see that the button stays yellow (=pressed) for 10 seconds even if you only pressed very shortly. Also: if you overdo it android os will become angry and post the user if he wants to force-close your app.
So what happens on handler.postDelayed?
Android will very quickly open a thread that runs in the background parallel to your UI thread. So in some nanoseconds it has done that and will execute the next command in UI thread (in the example above it is Log.d). In the background it will wait and count the millis until time is up. Then any code that is inside the runnable.run method will again be executed in the ui-thread after the wait.
Note also: postDelayed will not be super precise with the wait time as usually the ui-thread is quite buisy and when the wait time is up it may have something else to do. Your runnable code will be added to a queue and executed when ui-thread is ready again. All this happens without you having anything to do about it.
Also:
Remember to work with try/catch inside the runnable.run as many things can happen while waiting - for example user could press Home button closing your app - so the ui-element you wanted to change after the wait could already been destroyed.
I am trying to show invisible ImageViews on OnCreate method. I have a bunch of ImageViews that creates a tree image (E.g. ivStem, ivRoot... etc). These ImageViews are invisible at first, but after a few second, one will show up, another one again, and again... But the problem is I am having a hard time how to do it. I've tried the code below.
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
ImageView ivLogoText = (ImageView) findViewById(R.id.ivLogoText);
final double secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class));
//What should I put here?
if (secondsDelayed == 2000) {
ivLogoText.setVisibility(1);
} ...//more if statement here
finish();
}
}, (long) (secondsDelayed * 1000));
Overall, I have 5 seconds delayed. What I want to happen is set the visibility of this ImageViews according to that delay. How can I do this? I believed this is just an easy question but I've been searching for an hour and still can't find solutions. Thanks in advanced!
I am having a application which have a splash screen. My problem is i need a splash screen with the pinwheel spinner(progress bar). I have also added the android java code.
Java code
package com.SSF;
import android.os.Bundle;
import android.widget.Toast;
import com.worklight.androidgap.WLDroidGap;
public class SSF extends WLDroidGap {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
/**
* onWLInitCompleted is called when the Worklight runtime framework initialization is complete
*/
#Override
public void onWLInitCompleted(Bundle savedInstanceState){
super.loadUrl(getWebMainFilePath());
// Add custom initialization code after this line
}
}
Having a splash screen is redundant, and should be avoided unless maybe it's the first run of the app. Users like to open the app and start using it right away.
Only really heavy apps (mostly games ) need to load a lot of things, but even there, there are plenty of optimizations to make it short (just load what it needs in the near future, for example).
Anyway, for the progress bar, just create a layout with a progress bar view in the middle, use "setContentView" on it, and that's it...
You can also customize the progress bar by yourself, for example using this post.
You can try this code. please
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);
// interesting music
/**
* Gets your sound file from res/raw
*/
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
Best of luck!
try below code:-
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
// show progress bar here
sleep(5*1000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),FirstScreen.class);
startActivity(i);
//Remove activity
finish();
// hide progress bar here
} catch (Exception e) {
}
}
};
// start thread
background.start();
for more info see below link:-
http://androidexample.com/Splash_screen_-_Android_Example/index.php?view=article_discription&aid=113&aaid=135
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
Up until Worklight 6.2 it was not possible to customize the splash screen in a Worklight-based Android application, be it adding a spinner, extending the time the splash is displayed or creating an entirely customized experienced.
Starting Worklight 6.2 the entire flow is customizable if the developer so chooses. This is documented at: Managing the splash screen in an Android-based hybrid application, which also provides various code examples.
BTW, you already asked about this... Splash Screen with loading in four environment(android,ios,blackberry and windows) using html coding or common plugin for hybrid apps
Here is my code:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start();
}
private void start() {
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
Button button = new Button(this);
layout.addView(button);
button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x0000FF00)); // green
button.invalidate();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x000000FF)); // blue
button.invalidate();
}
}
This just displays a blue button after 3 seconds; it never shows it as green.
I think that if my brain worked properly, I could figure out the answer from one of these posts:
Why isn't view.invalidate immediately redrawing the screen in my android game
How to force a view to redraw immediately before the next line of code is executed
How to force an entire layout View refresh?
You should not sleep on the UI thread, as this will hang your interface. Instead, after initially setting the colour to green, you could create a Handler (on the UI thread, so it will use that thread to handle messages), then use Handler.postDelayed to send a message that will be handled in 3 seconds. In the Runnable you pass to postDelayed you can set the colour to blue.
You're sleeping the main UI thread virtually instantly after setting the colour. This means that the thread is locked up before it gets a chance to do redrawing.
One solution to this is to use threads. Set the background colour, then use another thread to sleep which on completion calls the main (UI) thread to change the colour again.
You can try looking up AsyncTask or Threading.
i'm making a 15 puzzle (http://en.wikipedia.org/wiki/Fifteen_puzzle) game, and i have an activity for user to select the background image, and then i pass that image to a new activity in order to scale and crop it.
Now i want to show the solution to the user first for 3 secs then it would shuffle, i'm using code like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...skip the code that gets the image and scales it
start();
}
then in my start() funcion:
public void start() {
//the createPuzzle function would create all the Views(tiles)
//and add them to the root LinearLayout using addView() function
createPuzzle(game.getBoardConfig(), dimension);
//i was trying to sleep here
shuffle();
}
i used:
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
or:
SystemClock.sleep(3000);
but neither of them worked properly, they all paused the the thread right after i selected the image and when it was paused i can't see the new activity and the tiles i created. When the thread resumes, it was already showing the shuffled puzzle.
I've been looking at the documentation for quite a while but still can't figure out what's wrong with my code, thanks for helping!
Don't make the UI thread sleep because that will lock up the entire UI thread which is a no-no. Instead, use a Handler to post a runnable with a delay...like
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
shuffle();
}
}, 3000);