Trying to add splash screen - android

I have been trying to add splash screen like this one in my android app but the app keeps crashing whenever i try to run it. here are the codes
activity_splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".Splash"
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:background="#drawable/cricket"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
**AndroidManifest.xml**
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<activity
android:name=".Splash"
android:label="Splash"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
Here is my java code for splash activity
Splash.java
public class Splash extends AppCompatActivity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler() {
public void postDelayed(Runnable runnable, int splashTimeOut) {
}
#Override
public void close() {
}
#Override
public void flush() {
}
#Override
public void publish(LogRecord record) {
}
}.postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}

update your handler
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}, SPLASH_DISPLAY_LENGTH);
}

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}

Related

Login Activity gets Crashed after Splashing Activity

I am working on an android project in which there are Login, Register, Main Activity and also a Splash Activity.
So in that, except Splash Activity, all activity gets crashed.
First Splash Activity gets executed then Main Activity gets Crashed.
Here is the Splash Activity:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread()
{
#Override
public void run() {
try
{
sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
Intent mainintent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(mainintent);
}
}
};
thread.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Here is the Manifest.xml file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.connect2every1.MainActivity">
</activity>
<activity android:name="com.example.connect2every1.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Login Activity, Register Activity, Main Activity, must be get executed
the main motive is that
after Splash Activity Login Activity must be executed.
It is better to use the following code
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainintent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(mainintent);finish()
}
}, 2000);
After that remove finish() from onResume()
Did you register your every Activity in AndroidManifest.xml?
If you did, you won't be able to switch UI from a non-UI thread without a Handler.
So in order to get your code work, you'll need to initialize a Handler in UI thread and post a Runnable instance with or without delay in UI or non-UI thread.
Here is the Code
public class SplashActivity extends AppCompatActivity {
private mHander Handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mHander = new Handler();
}
#Override
public void onPostCreate(){
mHander.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,LoginActivity.class));
finish();
}
},5000);
}
}
and also remove finish(); from onPause() method, because your current activity automatically gets closed when Runnable executes.
Hope this helps!
You can't change UI from worker thread, only Main thread can change UI, so use runOnUiThread when starting activity.
Thread splash=new Thread(){
public void run() {
synchronized (this) {
try {
wait(2000);
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
splash.start();
First change your Activity name MainActivity to LoginActivity from Manifest file.
<activity android:name="com.example.connect2every1.LoginActivity ">
</activity>
instead of
<activity android:name="com.example.connect2every1.MainActivity">
</activity>
than, Use handler Class for splash screen
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,LoginActivity.class));
finish();
}
},4000);

Splash Screen Activity

I added splash screen to my application and my code looks as follow:
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
}
My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vdovin.currencyratesapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".application.CurrencyApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".screens.splash.SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".screens.main.CurrencyExchangeActivity">
</activity>
</application>
</manifest>
So I faced with next problem:
If I hide my app with home button when loading splash screen, then when I open app again, splash screen activity not call CurrencyExchangeActivity. I understand that it appears because method onCreate() invoked only once, but I can't put it in onResume() because when I open my app again it shows me the splash screen again. But I want to show CurrencyActivity, like google's apps(maps, sheets etc...)
You cannot do that simply because you're setting the splash screen image as background of theme of your Activity. That's window level background which would show up regardless of your navigation or anything.
If you really want to show splash screen only once in app lifetime then you'll have to do it in not the right way and have that image as part of your layout and inflate that layout using setContentView. Now when you come again in the app, you will call your CurrencyExchangeActivity even before setContentView of splash activity and which will just show a black window background and directly show up CurrencyExchangeActivity.
Let me know if this makes sense or not. I can elaborate more if needed
Please try this way. I haven't tested your code. My sway of using the splash will be like below:
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
loadNext();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Thread() {
#Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
}.start();
}
protected void loadNext() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
}
you have forgot to add
setContentView(R.layout.splash);
add this it will work
Maybe you could try the following:
#Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
This will skip the delay when reopening the app after pressing the home button.
I usually use a postDelayed Runnable.
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
// 5 sec
handler.postDelayed(runnable, 5000);
}
#Override
protected void onPause() {
super.onPause();
// 5 sec
handler.removeCallbacks(runnable);
}
}
I would like to suggest to keep the splash screen showing mechanism in the CurrencyActivity in your case. Here's the pseudo code.
I've added proper comments in the code. Please check.
public class CurrencyActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000; //for testing i use 5 seconds
private Handler handler = null;
private boolean showSplash = true; // True by default
private ImageView splashImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency_activity);
// Get a image overlaying the other views in your currency layout
splashImage = (ImageView) findViewById(R.id.splash);
if(showSplash) showSplashScreen();
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onPause() {
super.onPause();
showSplash = false; // Set the variable to false when you take this in background
}
#Override
protected void onResume() {
super.onResume();
// Check if the variable is false and then set the visibility to GONE
if(!showSplash) splashImage.setVisibility(View.GONE);
}
private void showSplashScreen() {
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
splashImage.setVisibility(View.GONE);
showSplash = false;
}
}, DELAY_MILLIS);
}
}
Try this out--
SplashScreenActivity-
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
}
For manifest--
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CurrencyExchangeActivity"/>
</application>
i tested this code for if you had the problem that when the home screen button is pressed the splash screen is displayed again then this would work....

android -splash screen shows blank

The splash activity shows just blank screen. Code is below :
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class SplashScreen extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
try {
Log.v(" gonna AsyncTask"," execute AsyncTask");
new AsyncTask<String, String, String>() {
#Override
protected void onPreExecute() {
Log.v(" PreExecute AsyncTask"," execute AsyncTask");
}
#Override
protected String doInBackground(String... arg0) {
Log.v(" in doInBackground"," execute AsyncTask");
try {
Log.v(" sleep doInBackground"," execute AsyncTask");
Thread.sleep(SPLASH_TIME_OUT);
} catch (Exception e) {
Log.v(" Exception "," execute AsyncTask");
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String unused) {
Log.v(" onPostExecute "," execute AsyncTask");
Intent i = new Intent(SplashScreen.this,
MainActivity.class);
Log.v(" starting Activity "," execute AsyncTask");
startActivity(i);
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}// end of onCreate
}// end of class SplashScreen
The XML layout file has :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.istiaqueahmed.archeology.SplashScreen">
<ImageView
android:id="#+id/splash_img"
android:layout_width="170dp"
android:layout_height="112dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:src="#drawable/splash"
/>
<TextView
android:id="#+id/developed_by"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="#string/developed_by"
android:layout_below="#id/splash_img"
android:textStyle="bold"
android:textSize="13sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
/>
</RelativeLayout>
Android manifest file has -
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
>
<activity
android:name=".SplashScreen"
android:noHistory="true"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
........
</application>
And the style.xml has -
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Perhaps the activity needs some time for setContentViewto execute and meanwhile the app goes to the MainActivity.
How to prevent the app from showing the blank screen and instead show the splash screen for the intended time period?
Your App is freeze because you are sleeping main Thread.
Thread.sleep(SPLASH_TIME_OUT); // This will sleep your main thread.
Instead of AsyncTask use Handler with delay.
Use CountDownTimer instead of AsyncTask.
private static long SPLASH_TIME_OUT = 3000;
new CountDownTimer(SPLASH_TIME_OUT, 1000) {
public void onTick(long millisUntilFinished)
{
}
public void onFinish()
{
Intent i = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(i);
}
}.start();
There is no need to used AsyncTask here.
Please try this code which helps you.
public class WelcomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
startHandler();
}
private void startHandler() {
Runnable runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(i);
finish();
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, 3000);
}
}

How i directly go splash screen to sign in or register activity in android?

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
put this on MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent= new Intent(getApplicationContext(),SignIn.class);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
int secondsDelayed =1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(SplashActivity.this,LoginActivity.class));
finish();
}
}, secondsDelayed * 1000);
}
}
There are two tags you can use against any activity which you want open when user the app to open.
LAUNCHER as <category android:name="android.intent.category.LAUNCHER" />
MAIN as <action android:name="android.intent.action.MAIN" />
<activity android:name="com.example.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To jump from one activty to other use the below code.
Intent i = new (CurrentActivityName.this , NextActivityName.class);
startActivity(i);
Dont forget to add Next in manifest.
<activity android:name="com.example.activity.NextActivityName">
then you should use following code inside your Splash Activity onCreate method:
new Timer().schedule(new TimerTask() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, RegisterActivity.class));
}
}, 5000); //where 5000 is time in miliseconds for delay.

Application not loading after Splash screen

I am having an issue where the application does not load after including the Splash screen to the application. This is the code for the SplashScreen.java & the Manifest file. I am not sure what I am missing. I have gone over this several times and am unable to spot the error. I have gone thro' similar posts but could find the answer. Please help
public class SplashScreen extends Activity {
private long ms=0;
private long splashTime=2000;
private boolean splashActive = true;
private boolean paused=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {}
finally {
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}
};
mythread.start();
}
The Manifest file is as follows
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tvganesh.totalcontrol.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.tvganesh.totalcontrol.TotalControl"
android:label="#string/app_name" >
</activity>
</application>
After the Splash Screen which it display it is supposed to switch to TotalControl.class. But the I get the message "The Application stopped unexpectedly"
Can you let me know what I am missing?
Don't use Thread.sleep - you have no guarantee that it will start up again exactly after 100ms. Use a Handler instead:
getWindow().getDecorView().getHandler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}, splashTime);
I think this will work for you
Don't use threads to show the splash.
public class SplashScreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
MyCount counter = new MyCount(4000, 4000);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
counter.start();
}
public class MyCount extends CountDownTimer
{
public MyCount(long csecond, long countDownInterval)
{
super(csecond, countDownInterval);
}
#Override
public void onFinish() {
finish();
Intent intent = new Intent();
intent.setClass(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
}
}
}
I have solved the problem. This appears to be a known bug in AndEngine and the fix is add the following line to the Manifest file
android:configChanges="orientation|screenSize"
Thanks for all your help.

Categories

Resources