I want to make a splash screen for my android app. I have written following codes in welcomescreen.java in android studio. But after running the app, the app has stopped. :( What shall I Do now?
package com.mateors.welcomescreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class WelcomeScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(5000);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
private static int SPLASH_TIME_OUT = 1500;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
try the above code snippet
Try with following code:
package com.mateors.welcomescreen;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class WelcomeScreen extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Related
I am working on an app and while testing on my phone a noticed something weird: if I close the app while the splash screen is running, when the time of the before mention activity is over, the app opens again, even if I am in another app. Why does this happen?
package com.example.arlet.storemaps;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.Timer;
import java.util.TimerTask;
public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
Timer RunSplash = new Timer();
TimerTask ShowSplash = new TimerTask() {
#Override
public void run() {
//finishing splash screen
finish();
//starting main activity
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
}
};
RunSplash.schedule(ShowSplash, delay);
}
#Override
protected void onDestroy(){
timer.cancel();
timer.purge();
super.onDestroy();
}
}
Here's the code updated #Badran
Override the onDestroy Method in SplashActivity:
#Override
protected void onDestroy() {
//remove the handler or thread or etc... that is opening the another activity
//call timer.cancel()
//call timer.purge ()
super.onDestroy();
}
So Your code will be now :
package com.example.arlet.storemaps;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.Timer;
import java.util.TimerTask;
public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer RunSplash;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
RunSplash = new Timer();
TimerTask ShowSplash = new TimerTask() {
#Override
public void run() {
//finishing splash screen
finish();
//starting main activity
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
}
};
RunSplash.schedule(ShowSplash, delay);
}
#Override
protected void onDestroy(){
if(RunSplash != null){
RunSplash.cancel();
RunSplash.purge();
}
super.onDestroy();
}
#Override
protected void onPause() {
if(RunSplash != null){
RunSplash.cancel();
RunSplash.purge();
}
super.onPause();
}
}
I'm really new to coding and have almost no idea what I'm doing.
I need to get this splash screen to work but it keeps looping infinitely back between the splash screen and mainactivity and I have no idea why, I took the code off some YouTube video and the video had no explanation too so I'm stuck.
This is the code for mainactivity:
package sg.edu.tp.project1;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 1000;
private ImageButton Search01;
private ImageButton Mymusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Search01 = (ImageButton) findViewById(R.id.Search);
Mymusic = (ImageButton) findViewById(R.id.Mymusic);
}
{ new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent homeIntent = new Intent(MainActivity.this,
HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
public void gotoSearchpage(View view){
Intent intent = new Intent(this, searchpage.class);
this.startActivity ( intent );
}
public void gotoMymusic(View view){
Intent intent = new Intent(this, myMusic.class);
this.startActivity ( intent );
}
public void gotoPlaylist(View view){
Intent intent = new Intent(this, playlist.class);
this.startActivity ( intent );
} }
and this is the code for the splash screen:
package sg.edu.tp.project1;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class HomeActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
{ new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent homeIntent = new Intent(HomeActivity.this,
MainActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
}
First of all... use AsyncTask in Splash screen it is best practice to do background initialization and checks.
Second you don't need handler in MainActivity.
remove this code from MainActivity... it is redirecting u to HomeActivity.
{ new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent homeIntent = new Intent(MainActivity.this,
HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
and change your HomeActivity(Splash) like this..
public class HomeActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
new Loader().execute();
}
private class Loader extends AsyncTask<Void,Void, Void>{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
Thread.sleep(SPLASH_TIME_OUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//if(pd!=null) pd.dismiss();
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
HomeActivity.this.finish();
}
}
}
/* This is my launcher activity basically a splash screen which will wait for 5 sec but there is some problem with the intent..pls help
*/
package com.hfad.practice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Starting extends AppCompatActivity {
public void start()
{
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting);
Thread timer=new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent=new Intent(this,MainActivity.class); /*
here it is showing an error thats mentioned in the title*/
startActivity(intent);
}
}
};
timer.start();
}
}
Change
Intent intent=new Intent(this,MainActivity.class);
with
Intent intent=new Intent(Starting.this,MainActivity.class);
in your case this refers to the Thread subclass, while the first argument of Intent is a Context object
My problem is, if I set my splash as a Dialog by adding this line in the manifest there's a delay: android:theme="#android:style/Theme.Holo.Dialog.NoActionBar"
After the splash screen disappears it takes around 6 seconds or more to the main activity to appear.
How can I make this delay disappear?
Splash code:
public class SplashActivity extends Activity {
private final int DURATION = 3000;
private Thread mSplashThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashThread = new Thread() {
#Override
public void run() {
synchronized (this) {
try {
wait(DURATION);
} catch (InterruptedException e) {
} finally {
finish();
Intent intent = new Intent(getBaseContext(),
MainActivity.class);
startActivity(intent);
}
}
}
};
mSplashThread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notify();
}
}
return true;
}
}
Rather than using splash as dialogue you can do all your background work in a splash screen activity and then start your main activity..if you need dialogue animation then you can use animation like this.
overridePendingTransition( R.anim.come_up, R.anim.go_down );
By this you can manage your activity switching time.
i am not sure that this answer is appropriate but i have done it like this :
#Override
public void run()
{
// TODO Auto-generated method stub
startActivity( new Intent ( SplashActivity.this , MainActivity.class ) ) ;
}
#Override
protected void onStart()
{
super.onStart();
if(!isClosed)
splashHandler.postDelayed(this, "putYourTimeHere");
}
This is working for me at its best..
final int splashTimeOut = 3000;
Thread splashThread = new Thread(){
int wait = 0;
#Override
public void run() {
try {
super.run();
while(wait < splashTimeOut){
sleep(100);
wait += 100;
}
} catch (Exception e) {
}finally{
startActivity(new Intent(SplashScreen.this,LoginActivity.class));
finish();
}
}
};
splashThread.start();
this is the code for splashscreen display with some time delay
..
here set splash image in drawable in splash_screen.xml.
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class SplashScreen extends Activity {
LocationManager locationManager;
String provider, formattedDate, imeid;
double lat, lon;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_screen);
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formattedDate = df.format(c.getTime());
new Handler().postDelayed(new Runnable() {
public void run() {
Log.i("JO", "run");
Intent in = new Intent(SplashScreen.this,SecondActivity.class);
//in.putExtra("refreshclick", clickRefreshButton);
//in.putExtra("Current_Date", formattedDate);
// in.putExtra("ImeiId", imeid);
//Log.i("JO", "Current_Date"+formattedDate+";
ImeiId"+imeid+";lat"+lat+"lon"+lon);
startActivity(in);
finish();
}
}, 1000);
}
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, DURATION);
}
I have an activity thats waiting for the user press on an image. if the user dont press anything in 3 second i want the activity to close (finish()).
thats my code:
private final int delay = 3000;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_hangup);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
final ImageView pressToLaunchbrowser = (ImageView) findViewById(R.id.after_hang_up_image);
pressToLaunchbrowser.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // if we want to open the device.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
Thread.interrupted();
}
});
new Thread() {
public void run() {
try {
Thread.sleep(delay);
finish();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}}
My question is how can i wake the Thread if the press was made? i tried Thread.interrupted(); but its not working. the thread still waits 3 seconds if i press or not. Thanks!
Threads are not recommended to use in Android..use Handler to manage time-dependant operations..for example, instead of new Thread().., try
Handler handler = new Handler();
handler.postDelayed('runnable-that-will-finish-activity', 3000);
and in onclicklistener:
handler.remove('runnable-that-will-finish-activity') ;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
public class NewActivity extends Activity {
private final int delay = 3000;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_hangup);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
final ImageView pressToLaunchbrowser = (ImageView) findViewById(R.id.after_hang_up_image);
final Handler handler = new Handler();
handler.postDelayed(finishRunnable, delay);
pressToLaunchbrowser.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// // if we want to open the device.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
handler.removeCallbacks(finishRunnable);
}
});
}
private Runnable finishRunnable = new Runnable() {
#Override
public void run() {
finish();
}
};
}