Is it possible to set a default image during starting an application? On iPhone, I can set an image named "Default.png" to do that. But is it possible on Android?
(For information I ask that because I can't use a splascreen view or auto-activity to an other, because of videoview in the first activity.)
I dont understand a reason why not to use splashscrean
public class SplashScreenActivity extends Activity {
protected boolean _active = true;
private Thread splashTread;
protected int _splashTime = 2000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
// thread for displaying the SplashScreen
final SplashScreenActivity sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} catch (UnsupportedOperationException e1) {
// TODO: handle exception
} finally {
finish();
// start a new activity
Intent i = new Intent();
i.setClass(sPlashScreen, Your videoViewActivity.class);
startActivity(i);
}
}
};
splashTread.start();
}
// Function that will handle the touch
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (splashTread) {
splashTread.notifyAll();
}
}
return true;
}
}
Related
I have a splash activity that is displayed for 2 seconds before opening the main activity.
If the user presses the back button while the splash activity is being displayed, the splash activity closes. But a short time later, the main activity [which was triggered by the splash activity] opens up.
I don't want this to happen. I want the entire application to close if the back button is pressed while the splash screen is being displayed.
How do I accomplish this?
Edit:
below is my code for splash activity:
public class Splash2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Just add this code to your SplashActivity...
#Override
public void onBackPressed() {
super.onBackPressed();
android.os.Process.killProcess(android.os.Process.myPid());
}
or maintain one flag to determine to start Activity or not in Thread...
public class Splash2 extends Activity {
private volatile boolean interrupt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (!interrupt) {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public void onBackPressed() {
super.onBackPressed();
interrupt = true;
}
}
Try this-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Another way to do it is with runnable:
Declare Handler and Runnable in your Splash2 activity:
private Handler handler;
private Runnable startMain;
In onCreate(), initialize them and set the runnable to fire in 1.5 secs:
handler = new Handler();
startMain = new Runnable() {
#Override
public void run() {
Intent open = new Intent("com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
};
handler.postDelayed(startMain, 1500);
Override onBackPressed() and simply cancel runnable:
#Override
public void onBackPressed() {
handler.removeCallbacks(startMain);
super.onBackPressed();
}
That's it. If back key is pressed, it cancels runnable and your homescreen won't run.
I write a Splash Screeen to run at the boot time of application
public class SplashScreen extends Activity {
ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imgView = (ImageView) findViewById(R.id.imgSplash);
new Thread(new WelcomeScreen()).start();
}
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
imgView.setImageResource(imgID[i]);
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
}
It getting error "Sorry the application has stopped unexpectedly" . I don't know why . Somebody can help me ????
you can not set the resource for yuor ImageView inside a thread different from the UI Thread.
you can use runOnUiThread. It takes as paramter a runnable, and post it in the UI Thread queue. There, the UI thead takes it and update your ImageView. All in all your runnable will become:
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
final int resuorceId = imgID[i];
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(resuorceId);
}
});
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
You can not access your views from Thread.
You will need to put your code imgView.setImageResource(imgID[i]); in runOnUiThread
use like:
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageResource(imgID[i]);
}
});
Thanks
You can not change something in UI from non-UI thread so replace this you code:
imgView.setImageResource(imgID[i]);
to:
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(imgID[i]);
}
});
//try code this way...
public class SplashScreen extends Activity {
private Intent launchIntent;
private Thread splashThread; //used for perform splash screen operation
private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true; //used for touch event
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen); //Set splashscreen.xml here
try {
splashThread = new Thread() { // Creating Thread for splash the screen
#Override
public void run() { // run method implemented to perform threading operation
try {
int waitTime = 0; //counter for threading
do {
sleep(sleepTime); //delay for specific time
if (active)
waitTime += 100;
//write your image code here that display your no. of images
} while (active && (waitTime < splashTime)); //Check touch condition and counter
} catch (Exception e) {
// to handle runtime error of run method
Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage
}
finish(); //finish current activity
startJustCoupleActivityScreen(); //Call below defined function
}
};
splashThread.start(); //start thread here
} catch (Exception e) {
message("SplashScreen : "+ e.toString()); //Call static method of class ToastMessage
}
}
public void startJustCoupleActivityScreen() {
launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen
startActivity(launchIntent); //start new activity
}
#Override
public boolean onTouchEvent(MotionEvent event) { //onTouch Event
//on touch it immediate skip splash screen
if(event.getAction()==MotionEvent.ACTION_DOWN) active=false; //Check Touch happened or not
return true;
}
public void message(String msg)
{
Validation.displayToastMessage(SplashScreen.this, msg); //display Error Message
}
}
This question already has answers here:
Splash screen while loading a url in a webview in android app
(3 answers)
Closed 9 years ago.
I want to load a website (in WebView), but it loads too slow and show a white screen. I want to show a splash screen and in the background load the WebView. After few seconds I want to close the splash screen and show the site that should be ready by then. How can I do it?
Thanks!
I use this code for a splash screen
public class SplashActivity extends Activity {
private boolean isBackButtonPressed;
private static final int SPLASH_DURATION = 2000; // 2 seconds
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.SplashTheme);
getWindow().setWindowAnimations(0);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
// make sure we close the splash screen so the user won't come
// back when it presses back key
finish();
if (!isBackButtonPressed) {
// start the home screen if the back button wasn't pressed
Intent intent = new Intent(SplashActivity.this, FragmentActivity.class);
startActivity(intent);
finish();
}
}
}, SPLASH_DURATION);
}
#Override
public void onBackPressed() {
super.onBackPressed();
isBackButtonPressed = true;
}
}
You can use the following code for it.
public class Splash extends Activity {
/** Called when the activity is first created. */
private boolean mSplashActive = true, mPaused;
private long mSplashTime = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Thread(){
public void run(){
try {
long ms = 0;
while(mSplashActive && ms < mSplashTime) {
sleep(100);
if(!mPaused) {
ms += 100;
}
}
if(Resources.getResources().isUserRegistered(Splash.this)) {
//user is registered so launch main screen
} else {
//user is not registered launch welcome wizard.
Intent intent = new Intent(Splash.this, WelcomeScreen.class);
Splash.this.startActivity(intent);
}
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
#Override
public void onPause(){
super.onPause();
mPaused = true;
}
#Override
public void onResume(){
super.onResume();
mPaused = false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
mSplashActive = false;
}
return true;
}
}
in the webview class you can set something like this.
i setup this for you:
public class WebViewActivity extends Activity {
WebView webView;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
progressDialog = new ProgressDialog(WebViewActivity.this);
progressDialog.setTitle("here your title");
progressDialog.setMessage("message here");
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
// here you can display an imageview fullscreen
// or show and progressdialog
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
// here you can remove the imageview and the user can continue using the webview
// or hide the progressdialog
progressDialog.dismiss();
}
});
}
}
if you have any questions let me know
public class SplashScreen extends Activity {
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread() {
#SuppressWarnings("deprecation")
#Override
public void run() {
try {
synchronized (this) {
// Wait given period of time or exit on touch
wait(5000);
}
} catch (InterruptedException ex) {
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}
I made a splash image to show at the start of my activity..
The image show perfectly.But the problem is when i call this
public class SplashImageActivity extends Activity {
protected boolean active = true;
protected int splashTime = 5000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(active && (waited < splashTime)) {
sleep(100);
if(active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
startActivity(new Intent(SplashImageActivity.this,Myapps.class));
finish();
//startActivity(new Intent("com.splash.com.MyApps"));
//startActivity( new Intent(getApplicationContext(), Myapps.class));
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
active = false;
}
return true;
}
}
go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this
what's the problem?
No need to call stop() and call finish() after starting activity
finally
{
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
I use thread to show the Splash screen, and it works for me:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
wait(4000);
}
}catch(InterruptedException ex){
}
finish();
Intent i=new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
interrupt();
}
};
mSplashThread.start();
}
Please try below code..
public class Splashscreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread t2 = new Thread() {
public void run() {
try {
sleep(2000);
startActivity( new Intent(getApplicationContext(), Exercise.class));
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t2.start();
}
}
No need to call stop() just call finish() after starting activity
finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
You can also use handler an postdelayed() to make a splash screen like below
public class SplashScreenActivity extends Activity{
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
final Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
}
}
You will show your splash screen for 5 seconds and then move to next Activity
first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........
as looks you try some thing like this link
public class tryAnimActivity extends Activity
{
/**
* The thread to process splash screen events
*/
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Splash screen view
setContentView(R.layout.janman);
final tryAnimActivity sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, TabsActivity.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
whats the problem with this code? On clicking, it crashes down. Also, the next activity does not start after the end of this activity. This is the activity used for displaying splashscreen.
I prefer to use Runnable element instead of Thread. My splash code for the activity below works well. I hope it helps you:
public class SplashScreenActivity extends Activity implements OnTouchListener {
private int SPLASH_DISPLAY_LENGTH = 3000;
private Handler splashHandler;
private Runnable splashRunnable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
findViewById(R.id.splash_img).setOnTouchListener(this);
splashHandler = new Handler();
splashRunnable = new Runnable()
{
#Override
public void run()
{
SplashScreenActivity.this.finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(mainIntent);
}
};
splashHandler.postDelayed(splashRunnable, SPLASH_DISPLAY_LENGTH);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
splashHandler.removeCallbacks(splashRunnable);
SplashScreenActivity.this.finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(mainIntent);
}
return true;
}
}
just remove stop() method it will work