This question already has answers here:
Android - Prevent white screen at startup
(18 answers)
Closed 4 years ago.
I created a splash screen for my application using an empty activity that stays visible for 3 seconds with a background image.
Usually, the application starts with a white screen before the background image becomes visible, however, some applications are already started with the "real" splash screen image.
How to implement this?
you can use splash screen in this way.
Add style for splash screen activity in styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/yourImageName</item>
</style>
Add that style as theme to your SplashScreenActivity in manifest file
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Remove setContentView() in SplashScreenActivity's onCreate() Method and use it as a java file which extends AppCompactActivity
The problem is because system process draws initial blank screen when launching the app, from documentation:
A common way to implement a themed launch screen is to use the windowDisablePreview theme attribute to turn off the initial blank
screen that the system process draws when launching the app. However,
this approach can result in a longer startup time than apps that don’t
suppress the preview window. Also, it forces the user to wait with no
feedback while the activity launches, making them wonder if the app is
functioning properly.
You can disable it with windowDisablePreview attribute, something like this:
<application
...
android:windowDisablePreview="true">
...
</application>
Splash Activity usually starts with the background.
Try this code, it might help you.
Code for your splash activity page.
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}}
In layout file, you need to include just Image which you want to see on the splash page.
<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:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/capture"
/>
</RelativeLayout>
And make sure you add the Splashscreen page before Main activity in Manifest.
<activity android:name=".SplashScreen">
Related
On clicking on android app icon, app takes lot of time to open and then white screen appears before launching MainActivity.
splash_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/splash_screen"
android:gravity="center" />
</layer-list>
styles.xml
<style name="LauncherLogoTheme" theme="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/sdp_launcher_background</item>
</style>
manifest file
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:noHistory="true"
android:theme="#style/LauncherLogoTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashScreenActivity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}
Please help on how to use splash screen in a correct way
If you have seen some apps using a splash screen, you must have noticed that they stay on the splash screen for 1 or half a second. Which is a standard for a splash screen. Mostly splash screen is white and an image generally with app icon/logo and app name.
Coming to your issue; the issue with your code is that you start another activity when the current activity is just created and just after that you are finishing it. I also don't see setContentView().
The recommended approach is to use a Handler and use a delayed callback to start the next activity and finish the current one.
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler(Looper.myLooper()).postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
},1000);
}
}
Delayed starting of next activity and finishing of the current for 1 Second(1000 ms). Which will allow the Splash activity to be created and then resumed(become visible to user) smoothly before it is finished.
I try to add welcome tutorial for users that install application for the first time. That activity need to be declare as Main in Manifest (or I miss something?). But if I choose any other activity else than main one (which is actual app), app shortcuts (Android 7.1) doesn't work anymore. It's interesting however that shortcuts are still available at custom launchers (Apex, Nova). Any idea?
(almost all) Google apps has welcome tutorial as well as launcher shortcuts. I can't get it how they did it?
A welcome tutorial does not have to be an activity. It could be some other sort of presentation (e.g., a fragment).
A welcome tutorial, even if it is another activity, does not have to be the launcher activity. The launcher activity could detect that it is the first run and started the tutorial activity.
Thank you for the answers CommonWare! Your statements helps me to find answer. So, I want to start an app which shows Splash screen, then Welcome tutorial. Also, app need working shortcuts on main screen as well as only one launcher icon. So, first, I declare Splash screen as main in Manifest.xml:
<activity
android:name=".SplashActivity"
android:noHistory="true"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
Then, Welcome (tutorial) activity:
<activity
android:name=".IntroActivity.WelcomeActivity"/>
After that, in SplashActivity.class checking first launch:
public static final String FIRST_APP_LAUNCH = "com.ips.test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, WelcomeActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}
Final result is as I wanted: app launch with Splash screen, then it runs Welcome tutorial. Next start will trigger Splash screen which will continue to main activity (app itself). When user click on shortcut in main screen it will get shortcuts, and in Launcher it will have just one application shortcut.
I'm just getting started with Android and was reading up BroadcastReceiver. Since the MainActivity was being used only to get the alarm time in seconds, it got me thinking whether layout XML files are must for every activity in Android. I mean, is it possible to have an app that when launched, shows no view, but successfully sets up a receiver?
The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:
An activity is a single, focused thing that the user can do. Almost
all activities interact with the user [...]
(see http://developer.android.com/reference/android/app/Activity.html)
Related SO question: https://stackoverflow.com/a/12817384/534471
To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The code would look like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
finish();
}
}
You can implement an Activity without a UI. In the manifest you can specify android:theme="#android:style/Theme.NoDisplay". Take a look at this
You can also implement a Service which does not have any UI so you do not need layout inflation. Service just runs in background and shows no views.
Take a look at Android Training and API Guide to learn more about Services
I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.
First of all let me tell you guys that I am completely newbie to Android app development.
I want to know how to launch home screen and then application home screen.
i.e whenever you click on some app, first it will show some logo screen and then automatically jump to home screen of the app.
I have my home screen of the app running, I just want to add some kind of logo screen when user click on the app.
I did little bit research on this and I found out
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If I add this to logo activity then how to call app_home_screen activity automatically
Hope I am clear enough to explain the question.
You need to:
Add a new Activity SplashActivity which will be the start Activity of your app.
When your app starts, launch your SplashActivity and wait for some time.
When you finish waiting, start your MainActivity and finish the SplashActivity.
Here is a good example:
SplashScreenActivity:
public class SplashScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout); //You need to define it in your layouts
final int welcomeScreenDisplay = 3000; //splash lasts for 3 sec. You can change it
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreenActivity.this, MainScreenActivity.class));
finish();
}}
};
welcomeThread.start();
}
}
And don't forget to add the activities to your Manifest file:
<activity
android:name=".SplashScreenActivity"
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=".MainScreenActivity"
android:label="#string/app_name" >
</activity>
Although the other answers certainly work, here is an alternative to avoid having a separate activity just for your logo:
Place your ImageView as you would like (i.e. covering your full screen) before the other elements in your main activity layout (they will have an initial layout outside of the screen).
After calling setContentView in onCreate, run SystemClock.sleep(LOGO_TIME), where LOGO_TIME is an integer representing the number of milliseconds you want your logo to appear.
Finally, call setVisibility(View.GONE).
Your ImageView will be gone, and the other layout elements will fall into its place.
One solution you could attempt is to load the logo as your main activity, and have the application pause for a few seconds before calling the 2nd activity, your 'true' home page.
To start that 2nd activity you'll use 'Intent' such as
Intent myIntent = new Intent(myFirstActivity.this, realHomePage.class);
myFirstActivity.this.startActivity(myIntent);
Just make sure to extend the Activity class in your home page class
Have the main activity that gets called when your app icon is clicked on be an activity which has a view with only your logo on it. Something simple, just a RelativeLayout and an ImageView. After a time delay, have this activity start your actual main activity with an intent.