I am making an app and i am using sqlite database. On the first launch of my application and on its onUpdate, i am add data to sqlite db from the xml file. Consequently, on the first launch of my app, it shows me white screen ~15 seconds. I want to show my full screen), ~15 seconds. How i can do that?
You can use SplashActivity display your Image, use Hander.postDelayed() to delay this screen 15s before startActivity to main screen.
Here is example:
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 15000;
/** 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.*/
//Load data
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Related
This question already has answers here:
How do you display a Toast from a background thread on Android?
(14 answers)
Closed 7 years ago.
my application always crash whenever it reach Toast(parent) part. i tried emptying the entire run() and there were no problem.
this codes work fine in emulator but not on device.
please ignore the Toast if you would like to, my main problem is not the Toast but its the codes onwards. they crash on device.
im merely using the Toast to know where the application crashed.
public class LoadingActivity extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;
Intent intent;
LoadingActivity parent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_loading);
intent = getIntent();
parent = this;
ImageView logo = (ImageView) findViewById(R.id.imageView);
TextView splash = (TextView) findViewById(R.id.splash);
splash.setText(intent.getStringExtra(Constant.SPLASH_TEXT));
TranslateAnimation animator = new TranslateAnimation(logo.getX(), logo.getX(), logo.getY(), logo.getY() + 300);
animator.setDuration(2000);
animator.setFillAfter(true);
logo.startAnimation(animator);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
//error on here
Toast.makeText(parent, "im still fine", Toast.LENGTH_LONG).show();
/* Create an Intent that will start the Menu-Activity. */
Intent nextIntent = new Intent(parent, HomeActivity.class);
nextIntent.putExtra(Constant.LOGIN_USERNAME, intent.getStringExtra(Constant.LOGIN_USERNAME));
parent.startActivity(nextIntent);
Toast.makeText(parent, "passed", Toast.LENGTH_LONG).show();
LoadingActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
You cannot use Toast(any UI related elements) inside background thread because worker thread doesn't access Ui elements , so you can use Activity.runOnUiThread(Runnable) , also you can use your Activity context to make your Toast.
Toast.makeText(LoadingActivity.this, "passed", Toast.LENGTH_LONG).show();
How to display a Toast inside a Handler/thread?
I found in this answer this code:
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html",5000);
And it works, but like this, result is:
Splash screen for 5 seconds
Black screen until the app is ready
index.html when app is ready
So I was wondering if there is any chance of running this
super.loadUrl("file:///android_asset/www/index.html");
As a callback of some ready function, is there a way?
-EDIT-
Changing it to 10 seconds doesn't show me the black screen but I would like to show index.html the exact same moment that the app is ready (not sooner, not much later :D)
// Show LOGO ,start to MainActivity that watting for some seconds
new Handler().postDelayed(new Runnable() {
public void run() {
/*
* Create an Intent that will start the Main WordPress
* Activity.
*/
//
RedirectMainActivity();
}
}, 4000);
Android does not provide any of native API to deal with Splash Screen
but you can use Handler to show fake splash screen.
//load the splash screen
super.loadUrl("file:///android_asset/www/someSplashScreen.html");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// splash screen successfully timeout
//start new activity or load html layout
super.loadUrl("file:///android_asset/www/index.html");
}
}, 4000);//timeout after 4 sec
Have you alredy tried this?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 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_LENGHT);
}
}
In your link to a previous question there is a further link to a Blog
It claims, that with version 1.8.0 of PhoneGap you can call navigator.splashscreen.hide();
Check the Blog (read thru all of it as it is a bit missleading on the first two paragraphs).
My app is loading the start page in 10 seconds. In that time of 10 sec android screen is blank.
In that time I want to add the loading screen. How to add it?
And tell me in app how to know the starting page is loading? And tell me how to do in my app?
use ProgressDialog.
ProgressDialog dialog=new ProgressDialog(context);
dialog.setMessage("message");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();
hide it whenever your UI is ready with data. call :
dialog.hide();
You can use splash screen in your first loading Activity like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread welcomeThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000); //Delay of 10 seconds
} catch (Exception e) {
} finally {
Intent i = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(i);
finish();
}
}
};
welcomeThread.start();
}
Hope this code helps you.
Please read this article
Chris Stewart wrote there:
Splash screens just waste your time, right? As an Android developer,
when I see a splash screen, I know that some poor dev had to add a
three-second delay to the code.
Then, I have to stare at some picture for three seconds until I can
use the app. And I have to do this every time it’s launched. I know
which app I opened. I know what it does. Just let me use it!
Splash Screens the Right Way
I believe that Google isn’t contradicting itself; the old advice and
the new stand together. (That said, it’s still not a good idea to use
a splash screen that wastes a user’s time. Please don’t do that.)
However, Android apps do take some amount of time to start up,
especially on a cold start. There is a delay there that you may not be
able to avoid. Instead of leaving a blank screen during this time, why
not show the user something nice? This is the approach Google is
advocating. Don’t waste the user’s time, but don’t show them a blank,
unconfigured section of the app the first time they launch it, either.
If you look at recent updates to Google apps, you’ll see appropriate
uses of the splash screen. Take a look at the YouTube app, for
example.
You can create a custom loading screen instead of splash screen. if you show a splash screen for 10 sec, it's not a good idea for user experience. So it's better to add a custom loading screen. For a custom loading screen you may need some different images to make that feel like a gif. after that add the images in the res folder and make a class like this :-
public class LoadingScreen {private ImageView loading;
LoadingScreen(ImageView loading) {
this.loading = loading;
}
public void setLoadScreen(){
final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2, R.mipmap.loading_3, R.mipmap.loading_4};
final Handler loadingHandler = new Handler();
Runnable runnable = new Runnable() {
int loadingImgIndex = 0;
public void run() {
loading.setImageResource(loadingImages[loadingImgIndex]);
loadingImgIndex++;
if (loadingImgIndex >= loadingImages.length)
loadingImgIndex = 0;
loadingHandler.postDelayed(this, 500);
}
};
loadingHandler.postDelayed(runnable, 500);
}}
In your MainActivity, you can pass a to the LoadingScreen class like this :-
private ImageView loadingImage;
Don't forget to add an ImageView in activity_main.
After that call the LoadingScreen class like this;
LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();
I hope this will help you
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 3000; //set your time here......
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
If the application is not doing anything in that 10 seconds, this will form a bad design only to make the user wait for 10 seconds doing nothing.
If there is something going on in that, or if you wish to implement 10 seconds delay splash screen,Here is the Code :
ProgressDialog pd;
pd = ProgressDialog.show(this,"Please Wait...", "Loading Application..", false, true);
pd.setCanceledOnTouchOutside(false);
Thread t = new Thread()
{
#Override
public void run()
{
try
{
sleep(10000) //Delay of 10 seconds
}
catch (Exception e) {}
handler.sendEmptyMessage(0);
}
} ;
t.start();
//Handles the thread result of the Backup being executed.
private Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
pd.dismiss();
//Start the Next Activity here...
}
};
Write the code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread welcomeThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000) //Delay of 10 seconds
} catch (Exception e) {
} finally {
Intent i = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(i);
finish();
}
}
};
welcomeThread.start();
}
I'm developing an Android app.
The app should show a Splash Screen at startup while checking if a file is updated. If the file is not updated, it launches an Async Task to update the file.
The problem is, the image of the Splash Screen only shows when the file actually needs updating. Else, a black screen shows while performing the check.
My SplashScreen activity:
public class SplashActivity extends Activity
{
private final static String placesFile = "places";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
}
#Override
protected void onResume()
{
super.onResume();
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
private void intentAndFinish() {
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
/**
*
* #return false if Places Data is too old
*/
private boolean isFileUpdated() {
int daysOld = 0;
File f = new File(this.getFilesDir().getAbsolutePath() +"/"+placesFile);
if(f.exists()){
System.out.println("existe");
}
Date d = new Date();
Date currentDate = new Date(System.currentTimeMillis());
d.setTime(f.lastModified());
if(currentDate.compareTo(d)>0)
daysOld = determineDifferenceInDays(d, currentDate);
return daysOld < Consts.PLACES_DAYS_OLD_QTY_PERMITTED?true:false;
}
private static int determineDifferenceInDays(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
long diffInMillis = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();
return (int) (diffInMillis / (24* 1000 * 60 * 60));
}
public void onResultFromAsyncTask(boolean finished) {
if(finished){
intentAndFinish();
}
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/splash_es"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
You are killing your Splash before it be on the Screen. Because you are killing it inside the onResume method. Look this piece of documentation:
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
You can use a handler, it is more elegant than Thread to solve this problem, but the main ideia is the same of #sheetal.
On onCreate method:
handler = new Handler();
On onResume method:
handler.postDelayed(new Runnable() {
public void run() {
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
}, 1000);
If your file is updated You are closing your activity in onResume and switching to main activity and the process will take fraction of milliseconds to do that..so it is very much possible you will never see your spash screen..If you want you can use
private void intentAndFinish() {
Thread.sleep(10000)
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
Just to catch up with your splashScreen.
See #bruno 's post...We should use a handler
You call to isFileUpdated() takes time put it in background
You asyntask should be something like this
public class PlacesService extends Asynctask<Void,Void,Void>{
boolean flag=false
public Void doInBackground(){
flag=isFileUpdated()
if (!flag){
return
}else {
// Do your file update
}
return
}
public onPostExcecute(){
intentAndFinish();
}
}
Hi guys I just want the program to display the layout main0 and stay for a few secounds then display layout main1 like the programs we see in any phone where an image or layout show up at the start of the program and then fade.
/**the main activity */
public class rdwt extends Activity implements OnClickListener{
Button b1;
Button b2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main0);
//Here
setContentView(R.layout.main1);
b1= (Button)findViewById(R.id.Button01);
b2= (Button)findViewById(R.id.Button02);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View v) {
if (v==this.b1){
Intent callwrite = new Intent(this, wto.class);
startActivity(callwrite);
}
if(v==this.b2){
Intent callread = new Intent(this, rfr.class);
startActivity(callread);
}
}
}
You need to read a technical article about Updating the UI from a Timer
I think there are particularly two different approaches.
1) use a android.os.Handler and send a delayed message
2) use a timer and a timer task to update your layout after a specific amount of time
Code example:
TimerTask bla = new YourTask();
Timer timer = new Timer();
timer.schedule(bla, 1000);
And your class YourTask:
public class YourTask extends TimerTask {
public void run() {
updateYourLayout();
}
}
Edit: However I think you're looking for a splashscreen. This simple tutorial explains how to do it: http://www.anddev.org/simple_splash_screen-t811.html
// Place the following line in your code.
timer(3500); // Waits 3.5 seconds before moving on to the next activity.
public void timer(int counter){
new Handler().postDelayed(new Runnable(){
#Override
// counter is in milliseconds.
public void run() {
Intent mainIntent = new Intent(THISCLASSNAME.this,CLASSNAMETOJUMPTO.class);
startActivity(mainIntent);
finish();