I wanna make splash screen while loading the main activity.
So, I added Splash activity.
package com.originerd.tau;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread logoTimer = new Thread(){
#Override
public void run() {
// TODO Auto-generated method stub
try {
Intent i = new Intent(Splash.this, Main.class);
startActivity(i);
sleep(4500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
}
and activity_splash xml.
<RelativeLayout 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/splash" />
But when I executed it, the screen shows just black window and shows main activity after 3 seconds. I think it is becuse of image loading time of splash activity. So I added sleep(1000); before Intent i = new Intent(Splash.this, Main.class); line. And it works, but I think it is not that good solution.
I wanna know what is good solution at this situation. The purpose is showing up image while main activity is preparing the contents(It takes around 3 seconds). If there are any solutions(Loading image) instead of splash screen, please let me know.
Instead of using sleep method in Thread you can do same easily using Handler.postDelayed as:
Handler handler = new Handler();
handler.postDelayed(runnable, 1000);
Runnable runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, Main.class);
startActivity(i);
handler.removeCallbacks(runnable);
}
};
How large (bytes) is your image? Perhaps the delay could be minimized by shrinking the image size/complexity with graphics tools. As a test, you could try a single color image which should be quite small. That would at least tell you whether the delay is a function of image size.
Related
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).
I have two activity. I want to go from A to B with fixed time. After going to B i want to come back to A with fixed time. i have searched for it but could not understand where to start. Should i use thread or timer or handler.
I am trying to understand the below codes:
Handler activityChanger = new Handler();
activityChanger.postDelayed(new Runnable(){
startActivity(new Intent(this,about.class));
}, 10000);
It would be a great help if anyone can give me an example.
I have added below code.
Thread switchToActivityB = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
Intent intent = new Intent(this, About.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
});
switchToActivityB.start();
The place to put the code depends of how you want to do. For example, if you want to do this everytime you are in Activity A (that means that when you go to B and then press go back, after a fixed time it will go back to the activity B again), you should put the code in onResume() method.
If you just want to do this once, just put it in onCreate() method
Thread switchToActivityB = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000); // The fixed time in milli seconds
Intent intent = new Intent(activityA.this, activityB.class);
startActivity(intent);
finish();
} catch (Exception e) {
// Catch Exception
}
}
});
switchToActivityB.start();
Add this code to Activity A, in onCreate() method
Do the same in Activity B, in onCreate() method
Thread switchToActivityA = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000); // The fixed time in milli seconds
Intent intent = new Intent(activityB.this, activityA.class);
startActivity(intent);
finish();
} catch (Exception e) {
// Catch Exception
}
}
});
switchToActivityA.start();
For what you described you should use that either on onStart or onResume methods. If you place it on onCreate it will run just once, as your activities don't get destroyed.
You can use any of the options you described. StartActivity Is a safe method, so you can call it from outside your UI Thread.
You can put the above code in onResume() of 1st activity. Similarly, put the same code with little modification of activity name in "about.java" onResume()..
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();
}
Iam an newbie to android.I don't know whether this question may sound silly but i didn't find any solution.Please bare me. I had created an application which will first loads the app logo. I need to call another activity after this without using any click event.can anybody help me out wit this? and also i need to know in windows we can place panels over another panel. Can we do the same ting android? If yes how can i achieve that? I know that in a layout we have to place views but my questions is can we design view over another view so that i can hide and show views whenever needed?
Thanks in advance
Using Timers or Threads is a horrible way to do this, you are inviting memory leaks into your app. Use Android's Handler instead:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// create Intent for next activity and call startActivity with it
}
}, 2000);
If you have a reference to your content view, use contentView.getHandler() instead of creating a new one.
By the way, if this is for a personal project, consider NOT USING SPLASH SCREENS
You do not really provide enough information to give you a proper answer, but this will start a timer and when 5000 milliseconds has elapsed it will switch to another activity:
public class SplashActivity extends Activity {
private Timer t;
public void onCreate(Bundle b) {
super.onCreate(b);
t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, NextActivity.class);
startActivity(i);
}
}, 5000);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent();
intent.setClass(WelcomePage.this, HomePage.class);
startActivity(intent);
}
}
};
timer.start();
}
that should do the trick my friend!!
I wrote a splash screen. But the problem is as the splash screen shown in the screen a keyboard also invoked. What could be the possible reason for this??
Please find the image below
and the code gos as below for the activity
public class SplashActivity extends Activity{
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/* 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 splash2 = new Intent(SplashActivity.this,SplashActivityRed.class);
SplashActivity.this.startActivity(splash2);
SplashActivity.this.finish();
overridePendingTransition(R.anim.fadein,R.anim.fadeout);
}
}, SPLASH_DISPLAY_LENGHT);
}
}
and the 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:id="#+id/imageViewSplash" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/splash1" android:src="#drawable/splash1"></ImageView>
</LinearLayout>
PS: Sorry I had to hide the text and logo as they come under non-disclosure policy of the company I work for.
Please Remove the imageView from the Layout and add the following line the Linear Layout element
android:background="#drawable/splash1"
Like this:
<?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"
android:background:"#drawable/splash1">
</LinearLayout>
Hope this helps
Also Change the implementation of the SplashScreen with above layout. To change the time line change the value of welcomeScreenDisplay to wotever you want. Currently it is 4 seconds i.e. 4000 mili seconds.
public class SplashScreen extends Activity {
String status, subscriber;
Intent i = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("FIRST");
setContentView(R.layout.splash);
System.out.println("in HOME SCREEN");
/** set time to splash out */
final int welcomeScreenDisplay = 4000;
/** 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) {
System.out.println("EXc=" + e);
} finally {
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
finish();
}
}
};
welcomeThread.start();
}
}
Use this method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
probably you need to disable the touch event for image. However as remove the imageView from the Layout and add background image to the linear layout element
android:background="#drawable/splash1"