Adding a progress indicator to Google's new Splashscreen pattern - android

Having gone through this blog post Splash Screens the Right Way , i discovered Google's new approach to creating splashscreens.
But my problem with this new method is adding a progress bar/indicator to the splashscreen.
I found several solutions but they are all custom xml layouts instead of Drawables
I also tried creating a progress dialog in my Background_splash.xml which is inside my drawable folder but i got an error that it is not allowed.
My app starts from the splash-screen to App intro activity then to the Main activity.
Is there any way to add a progress indicator/bar to this type of splashscreen?
Below are my codes created using the Splash Screens the Right Way Tutorial
Background_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:drawable="#color/grey"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
AndroidManifest.xml
<activity
android:name="com.domain.app.appintro.SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>

The answer is you cannot! If you want to show the splash screen before the app loads, via the theme, it will have to be a static image. If you want to show a progress bar AFTER the app loads, for example if you need to download data, then you can add a progress bar to splash activity, which will be shown AFTER the static image and app finished loading. If you do not need to wait after app loads, then you are just wasting user time by adding another wait period, and making for a lousy app. All of this can be understood from the comments and replies from the article you linked.

Have you considered using AsyncTask to load the progressDialog. You might be trying to download something in the main thread which blocks splash screen to load.
public class MyActivity extends Activity {
private ProgressDialog pd = null;
private Object data = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
// Start a new thread that will download all the data
new DownloadTask().execute("Any parameters my download task needs here");
}
private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");
// This is where you would do all the work of downloading your data
return "replace this with your data object";
}
protected void onPostExecute(Object result) {
// Pass the result data back to the main activity
MyActivity.this.data = result;
if (MyActivity.this.pd != null) {
MyActivity.this.pd.dismiss();
}
}
}
}
Take this as a start point and put this as the inner class of your mainActivity.

As #lionscribe rightly said, there is no way to creating custom splash-screen with this approach. However, This http://saulmm.github.io/avoding-android-cold-startsmight help those who will like to animate elements in the new splash-screen approach.

Related

ActionBarSherlock dialog is not transparent and not dark

I am using ActionBarSherlock (ABS) and would like to add a dialog to my application as one can see in the ABS Demos Sample application provided by the project. The dialog sample look like this:
I created an activity myself. Here is relevant source code:
public class Dialog extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_DarkActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
For some reason, Android forces me to add setTheme() although the ABS sample does not do this. If I leave it out, I will run into the following error.
java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
My AndroidManifest.xml has the following settings, which are very similar to the ones from the ABS sample.
<activity
android:name=".activities.Dialog"
android:label="#string/title_activity_dialog"
android:theme="#style/Theme.Sherlock.Dialog" >
</activity>
The following screenshot shows how my dialog activity looks like.
I am using ActionBarSherlock 4.1.0 with maps support, the Android Support library v4.
Question: Can you figure out, why it looks so different?
Dark vs. light user interface
Transparent vs. opaque background
With and without actionbar
Update:
The ABS sample starts the dialog activity as follows:
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
Intent intent = (Intent) map.get("intent");
startActivity(intent);
}
I start the dialog activity as follows:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(R.string.title_menuItemDialogActivtiy)
.setIcon(R.drawable.ic_action_dialog)
.setIntent(new Intent(this, Dialog.class))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Meanwhile, I saw that this pattern is deprecated. Instead, I could use a DialogFragment. The question that occurs here: How can I integrate the fragment with the action menu item?
Alternative solution:
I decided to use a DialogFragment instead of an Activity as I estimate it to be more "future-safe". I basically followed the very informative tutorial Using DialogFragments (posted June 3, 2012), which I like to recommend as perfect starting point for any interest reader. Further, I like to add related and useful posts:
Validating user input
Softkeyboard vs. separate DONE button
The output you are seeing definitely comes from setting the Theme in Java code (which will override the value set in XML). I just stood up the following sample application (this is literally all there is) and replicated the issue by adding the extra setTheme() call.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Dialog"
android:theme="#style/Theme.Sherlock.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And the Dialog...
public class Dialog extends SherlockActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("This is a dialog!");
setContentView(text);
}
}
Now as to why you got an exception without that extra method, that's another matter (and quite strange). As you can see it should work with as little code as I provided above.
Perhaps make sure that both the library project and your project are being compiled with at least Android 4.0 (API 14) as this is a requirement of the library.
Beyond that, if you just want to show a Dialog in your application, does it need to be a themed Activity? This is not common. You can always create a simple Dialog or AlertDialog subclass to display as well. Take a look here for more information...
Try setting a theme for the application.
<application
android:theme="#style/Theme.Sherlock"
The dialogue should inherit the theme. If you've already set that then remove the android:theme tag in the activity declaration and the setTheme() call and see what happens. The reason you get the error without setTheme is because SherlockActivities must have one of the themes in the error message and you had it set to something else in the manifest setTheme() overrode this before you got into trouble.

Android activity-reset after picture taken (orientation?)

Well basically, I press a button, this opens up your default camera app by using the camera intent. After a picture is taken, it will save the things needed and redirect to another activity.
In this activity, I have an AsyncTask that can succesfully upload pictures. So what is my problem you may ask. My problem is that it re-creates my activity and therefore reset my ProgressDialog together with it. ( It runs the activity, does the aSyncTask, dies before it can finish it and re-creates my Activity to do the asynctask once again. )
It does not always do this. I think it does this because it changes the Orientation from the phone from Landscape to Portrait. ( I have a Samsung. When I go to the Camera it changes to landscape and when I finish it, it goes back to portrait. )
I've already done my homework and added these things to my manifest:
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" >
I've made sure to "lock" my app in the portrait orientation but I still see my app change orientation and I believe this is why my activity gets re-created.
I was planning to add all kinds of checks but I believe this is not the right way to handle this situation, since it sometimes does not re-create the activity.
The check I am talking about is to use:
protected void onSaveInstanceState(Bundle outState) {
outState.putString("started", "1");
}
Anyway, can somebody help me out? I just want it to load the activity without it self-destructing on me.
PS: The VM doesn't have any problems. The VM loads the activity and finishes it without re-creating it.
PPS: Did extra testing, on my Samsung if I keep it on landscape-mode it will work. So it is definately the camera that is destroying my activity with it's orientation change.
I had the same issue, turns out you also need to listen for screen size changes in API level 13 or higher as explained here; https://stackoverflow.com/a/11483806
android:configChanges="orientation|screenSize"
For this to fix, I had to use following in my manifest file:
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:configChanges="keyboardHidden|orientation|screenSize"
Try creating a fragment activity to handle displaying and updating the progress dialog
In the fragment activity make sure and set "setRetainInstance(true);" This will make sure it isn't destroyed when the main activity gets created/destroyed.
It's probably a good idea to put the entire image capture process inside this fragment, including the asynctask. Make sure you don't reference the parent activity's context from within the doInBackground() in the AsyncTask. If you do this and the orientation changes (i.e. the activity is destroyed) it will throw an error.
here's a rough example:
public class MyFragment extends FragmentActivity {
private ProgressBar mProgressBar;
private boolean mAsyncTaskActive = false;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
// grab reference to progress bar
mProgressBar = (ProgressBar) getActivity().findViewById(R.id.my_progress_bar);
// check to see if the async task is active and set the progress bar visibility accordingly
if (mAsyncTaskActive) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBarText.setVisibility(View.VISIBLE);
}
}
// this method is called from your main activity when the user does something (i.e. clicks a button)
// make sure you have already instantiated the fragment
public void startSomething() {
if (mAsyncTaskActive == false) {
mProgressBar.setVisibility(View.VISIBLE);
new MyAsyncTask().execute();
mAsyncTaskActive = true;
}
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
Context applicationContext;
#Override
protected Void doInBackground(String... params) {
// do stuff
publishProgress(//some number);
return null;
}
#Override
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
}
You should also take a look at how to implement fragments if you're not already familiar. The Android dev blog has a good post on DialogFragments, same priniciples. http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

How to hide action bar before activity is created, and then show it again?

I need to implement splash screen in my honeycomb app.
I use this code in activity's onCreate to show splash:
setContentView(R.layout.splash);
getActionBar().hide();
and this code to show main UI after sometime:
setContentView(R.layout.main);
getActionBar().show();
But before onCreate is called and splash appears, there is small amount of time when action bar shown.
How can I make action bar invisible?
I tried to apply theme to activity without action bar:
<item name="android:windowActionBar">false</item>
but in that case getActionBar() always returns null and I found no way to show it again.
Setting android:windowActionBar="false" truly disables the ActionBar but then, as you say, getActionBar(); returns null.
This is solved by:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.splash); // be sure you call this AFTER requestFeature
This creates the ActionBar and immediately hides it before it had the chance to be displayed.
But now there is another problem. After putting windowActionBar="false" in the theme, the Activity draws its normal Window Title instead of an ActionBar.
If we try to avoid this by using some of the *.NoTitleBar stock themes or we try to put <item name="android:windowNoTitle">true</item> in our own theme, it won't work.
The reason is that the ActionBar depends on the Window Title to display itself - that is the ActionBar is a transformed Window Title.
So the trick which can help us out is to add one more thing to our Activity theme xml:
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>
This will make the Window Title with zero height, thus practically invisible .
In your case, after you are done with displaying the splash screen you can simply call
setContentView(R.layout.main);
getActionBar().show();
and you are done. The Activity will start with no ActionBar flickering, nor Window Title showing.
ADDON:
If you show and hide the ActionBar multiple times maybe you have noticed that the first showing is not animated. From then on showing and hiding are animated. If you want to have animation on the first showing too you can use this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
// delaying the hiding of the ActionBar
Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
getActionBar().hide();
}
});
The same thing can be achieved with:
protected void onPostResume() {
super.onPostResume();
getActionBar().hide();
but it may need some extra logic to check if this is the first showing of the Activity.
The idea is to delay a little the hiding of the ActionBar. In a way we let the ActionBar be shown, but then hide it immediately. Thus we go beyond the first non-animated showing and next showing will be considered second, thus it will be animated.
As you may have guessed there is a chance that the ActionBar could be seen before it has been hidden by the delayed operation. This is actually the case. Most of the time nothing is seen but yet, once in a while, you can see the ActionBar flicker for a split second.
In any case this is not a pretty solution, so I welcome any suggestions.
Addition for v7 support actionbar user, the code will be:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
Using this simple code in your .class file to hide action bar
getSupportActionBar().hide();
If you are using ActionBarSherlock, then use Theme.Sherlock.NoActionBar Theme in your Activity
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.Sherlock.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Create two styles:
<style name="AppThemeNoBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
<style name="AppThemeBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">false</item>
</style>
Set AppThemeNoBar as application theme and AppThemeBar to the activity where you want to show the ActionBar.
Using two styles you wont see the Action bar while views are loading.
Check this link Android: hide action bar while view load
Hi I have a simple solution by using 2 themes
Splash screen theme (add it to the manifest):
<style name="SplashTheme" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:windowBackground">#color/red</item>
</style>
normal theme (add it in your activity by setTheme(R.style.Theme)):
<style name="Theme" parent="#style/Theme.Holo"> <item name="android:windowBackground">#color/blue</item>
</style>
To support SDK 10:
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme);
super.onCreate(savedInstanceState);
...........
...........
}
I was also trying to hide Action Bar on Android 2.2, but none of these solution worked. Everything ends with a crash. I checked the DDMS LOg, It was telling me to use 'Theme.AppCompat'.At last I Solved the problem by changing the android:theme="#android:style/Theme.Holo.Light.NoActionBar"Line
into android:theme="#style/Theme.AppCompat.NoActionBar"and it worked, but the the Interface was dark.
then i tried android:theme="#style/Theme.AppCompat.Light.NoActionBar" and finally got what i wanted.
After that when I Searched about 'AppCompat' on Developer Site I got This.
So I think the Solution for Android 2.2 is
<activity
android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And Sorry for my bad English Like Always.
Best result to me was to create an activity with ThemeNoTitleBar and without content as launcher. Then this activity call directly to the other.
Of course if you want you can add content to the Splash Activity but in my case I just wanted to show application directly.
Manifest:
<activity
android:name="com.package.SplashActivity"
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:
public class SplashActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//start StartActivity
Intent intent = new Intent(this, StartActivity.class);
startActivity(intent);
finish();
}
}
If you use one Activity included a splash screen, then you can do this if you use SherlockActionBar
getSupportActionBar().hide();
After the splash screen you can show it again with ...
getSupportActionBar().show();
It should be the same with native ActionBar of Android.
#Clerics solution works. But this appears to also be an issue with some of googles native apps: googles, play store, talk. Also other big apps like skype.
EDIT: Bellow solution have given me problem for actionbarsherlock on api < 4.0, the reason being setTheme doesn't work pre ice cream sandwich
Add following in your manifest within you application or activity tags:
android:theme="#style/Theme.NoActionBar"
And then in your main activity:
// Set theme
setTheme(R.style.YOUR_THEME);
getSupportActionBar().setTitle(R.string.title);
// Start regular onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
best and simple
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
2015, using support v7 library with AppCompat theme, set this theme for your Activity.
<style name="AppTheme.AppStyled" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/md_indigo_100</item>
<item name="colorPrimary">#color/md_indigo_500</item>
<item name="colorAccent">#color/md_red_500</item>
<item name="android:textColorPrimary">#color/md_white_1000</item>
<item name="android:textColor">#color/md_purple_500</item>
<item name="android:textStyle">bold</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
For Splashscreen you should use this line in manifest and don't use getActionBar()
<item name="android:windowActionBar">false</item>
and once when Splash Activity is finished in the main Activity use below or nothing
<item name="android:windowActionBar">true</item>
Try this, it works for me:
Below gets rid of activity's title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
Below eliminates the notification bar to make the activity go full-screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
(Full Example Below)
Take note: These methods were called before we set the content view of our activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Sets Application to full screen by removing action bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// without this check, we would create a new fragment at each orientation change!
if (null == savedInstanceState)
createFragment();
}
this may be handy
add this to your manifest
android:theme="#android:style/Theme.Light.NoTitleBar"
cheers
The best way I find after reading all the available options is set main theme without ActionBar and then set up MyTheme in code in parent of all Activity.
Manifest:
<application
...
android:theme="#android:style/Theme.Holo.Light.NoActionBar"
...>
BaseActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.GreenHoloTheme);
}
This way helps me to avoid ActionBar when application start!
The solutions already posted came with the sideffect, that the first .show() call did not animate the ActionBar for me.
I got another nice solution, which fixed that:
Create a transparent drawable - something like that:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#00000000" />
</shape>
Set the actual actionbar background to a invisible custom view which you set on the actionbar:
getSupportActionBar().setCustomView(R.layout.actionbar_custom_layout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Set the transparent background for the actionbar in onCreate:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_transparent));
Imortant: Don't hide the actionbar immediately in onCreate, but with a little delay later - e.g. when the layout is finished with creation:
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
getSupportActionBar().hide();
}
});
Before your first .show() call set the custom view visible:
_actionbarRoot.setVisibility(View.VISIBLE);
getSupportActionBar().show();
In case you have null because you are using the support library, instead of getActionBar() you need to call getSupportActionBar().
Just add this to your MainActivity in the onCreate function.
val actionBar = supportActionBar?.apply { hide() }
Put your splash screen in a separate activity and use startActivityForResult from your main activity's onCreate method to display it. This works because, according to the docs:
As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity.
You should probably do this only if the argument to onCreate is null (indicating a fresh launch of your activity, as opposed to a restart due to a configuration change).
I had still error with null pointer and finally it helped when I called first getWindow().requestFeature() and then super.onCreate()
public void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getActionBar().show();
Just add this in your styles.xml
<item name="android:windowNoTitle">true</item>
Actually, you could simply set splash Activity with NoActionBar
and set your main activity with action bar.
The best way to do this is two make the first activity as blank activity and the content you want to put and then after some time fire another activity.
By following this way you can make the first activity as your splash screen without action bar or anything.
Heres my first activity
package com.superoriginal.rootashadnasim.syllabus;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class first_screen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
final Handler h=new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent=new Intent(getApplicationContext(),DrawerChooseBranchMainActivity.class);
startActivity(intent);
}
},2000);
}
}
After that you can put any of your activity as first activity
If you want no action bar in any screen then just add this in your styles
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Android studio provide in build template for full screen, if you use Android studio you can follow below step to implement full screen activity.
Done. Android studio did your job, now you can check code for full screen.
this is the best way I used
Go to java file, after onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Take instance of Action Bar
// using getSupportActionBar and
// if it is not Null
// then call hide function
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
}
I realise that posting links are not the best way to do things, but I highly recommend you read the following documentation from Google themselves. This is the official android doc on how to control your system ui (things like actionbar, nav bar etc). Unfortunately the info is too much to post directly, but after reading this you will understand exactly how to show and hide features no matter what version you are developing for, its so simple!
Incase the link ever changes, it can be found under the official android documentation under training -> getting started -> Best practices for user interface -> managing the system ui
https://developer.android.com/training/system-ui/index.html
you can use this :
getSupportActionBar().hide(); if it doesn't work try this one :
getActionBar().hide();
if above doesn't work try like this :
in your directory = res/values/style.xml , open style.xml -> there is attribute parent change to parent="Theme.AppCompat.Light.DarkActionBar"
if all of it doesn't work too. i don't know anymore. but for me it works.

How to make a splash screen (screen visible when app starts)?

I have a simple application, it starts, loads xml feed from the net, you can browse a list of news and then read details for a chosen news item. What I would like to do is have a splash screen, meaning as soon as you click application, it should display an image (app name in my case) and then display news list only after they've loaded.
I read about similar (I think) problems, and usually people say to use FrameLayout, but I can't really sort it out. I'm not sure if this can be done in the first activity that is launched, maybe I should just display this splash image in one activity and only then call activity displaying my news list?
I know that on iPhone you can set splash screen in app settings while developing, would be nice to have this functionality in android's app's manifest...
Android suggests you take advantage of using a splash screen when performing lengthy calculations on start up. Here's an excerpt from the Android Developer Website - Designing for Responsiveness:
"If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously. In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen." -- Android Developer Site
You can create an activity that shows a Progress Dialog while using an AsyncTask to download the xml feed from the net, parse it, store it to a db (if needed) and then start the Activity that displays the News Feeds. Close the splash Activity by calling finish()
Here's a skeleton code:
public class SplashScreen extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// set the content view for your splash screen you defined in an xml file
setContentView(R.layout.splashscreen);
// perform other stuff you need to do
// execute your xml news feed loader
new AsyncLoadXMLFeed().execute();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
}
#Override
protected Void doInBackground(Void... voids){
// load your xml feed asynchronously
}
#Override
protected void onPostExecute(Void params){
// dismiss your dialog
// launch your News activity
Intent intent = new Intent(SplashScreen.this, News.class);
startActivity(intent);
// close this activity
finish();
}
}
}
hope that helps!
I know this is old but for those of you who are still facing this problem, you can use this simple android-splash library to show your splash screen.
SplashBuilder
.with(this, savedInstanceState)
.show();
You can set SplashTask that will execute while the splash screen is displayed.

Android SplashScreen

I'm developing an application which basically downloads a lot of data at the start of the application itself and displays it in the ListActivity. What I'm planning to do is show a Splash Screen till the data is loaded.
Till now all my attempts have been futile. I tried anddev.org mentioned methods, but my problem is that the main Activity should start but The Splash Screen should be visible till I populate my ListActivity. So in short I have to go through the following steps:
Start my main activity.
Show the Splash Screen.
Keep running the process in background.
Exit the Splashscreen when processing completed and show the main List.
Hope you understand what it is like....
The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.
So your Activity's onCreate() method would simply create a ProgressDialog and show it. Then create the AsyncTask and start it. I would make the AsyncTask an inner class of your main Activity, so it can store the data it has downloaded to some variable in your Activity and close the ProgressDialog in its onPostExecute() method.
Not sure how to elaborate anymore without just showing the code, so here it is:
public class MyActivity extends Activity {
private ProgressDialog pd = null;
private Object data = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
// Start a new thread that will download all the data
new DownloadTask().execute("Any parameters my download task needs here");
}
private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");
// This is where you would do all the work of downloading your data
return "replace this with your data object";
}
protected void onPostExecute(Object result) {
// Pass the result data back to the main activity
MyActivity.this.data = result;
if (MyActivity.this.pd != null) {
MyActivity.this.pd.dismiss();
}
}
}
}
Obviously there are some pieces you need to fill in there, but this code should run and give you a good starting point (forgive me if there is a code error, I don't have access to the Android SDK as I'm typing this currently).
Some more good reading on the subject of AsyncTasks in Android can be found here and here.
just for reference this is the best way I found to make a splash screen:
http://android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html
I was searching for this for quite a while, from androids docs.. if you want to avoid those black screens, you need to create a theme with windowBackground so:
<resources>
<style name="Theme.Shelves" parent="android:Theme">
<item name="android:windowBackground">#drawable/background_shelf</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
And set this theme as the theme for your main activity... TADA, splashscreen from the first second.
If you want a complex background and not just an image that will be stretched to fill you can use Drawables, here is an example of a layer-list that will keep the logo centered with a black background:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#color/black">
</item>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/logo"
android:tileMode="disabled" >
</bitmap>
</item>
</layer-list>
Splash screen example :
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
}
Start my main activity.
Show the Splash Screen.
Keep running the process in background.
Exit the Splashscreen when processing completed and show the main List.
I tried this way, but the problem is; it will show the main activity before starting the splash screen activity.
I made it this way:
Start the Splash screen
When process gets completed, start the "main activity"
Do Not forget to handle the back button, so it should close the App ist will be pressed in the main activity. Otherwise will back to the splash screen (loop)
My Problem is how to disable "show the Menu of the Splash screen activity" by pressing the menu-button.
Edit:
Disable show menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// return super.onMenuItemSelected(featureId, item);
return false;
}
I know that this question is pretty old, and the OP may not need it anymore. But I just want to add this answer to help people who may need this to archive a splash screen.
Answer (only work on Android Oreo or greater versions)
Actually, in the newer version of Android (after Android Oreo), it already support built-in splash screen implement. That means you don't need to create extra activity to do that. You only need a drawable resource file for display.
Using this way is faster to your splash screen and soon show your content just after the initialization. But please note that this only work on Android Oreo or greater versions. On the previous version, it will show white instead of your splash screen (at least I think so).
You need this line in your AppTheme style:
<item name="android:windowSplashscreenContent">#drawable/YOUR_SPLASH_SCREEN_DRAWABLE</item>
This is a full example:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!-- Set your splash screen here, it accept a resource from drawable directory-->
<item name="android:windowSplashscreenContent" tools:targetApi="o">#drawable/splash_screen</item>
</style>
Reference
And for more informations about this attribute, see the official reference here: https://developer.android.com/reference/android/R.attr#windowSplashscreenContent
As it said, it is added in API level 26.
And a short extract of what it said:
Reference to a drawable to be used as the splash screen content of the window. This drawable will be placed on top of the windowBackground with its bounds inset by the system bars. If the drawable should not be inset by the system bars, use a fullscreen theme.
Note that even if no splashscreen content is set on the theme, the system may still show a splash screen using the other attributes on the theme, like the windowBackground.
You should use the Android Platform SplashScreen API for Splash Screens which you can provide an adaptive icon or an AVD along with controlling how long it is visible for. Check out the documentation
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Handle the splash screen transition.
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
and create a splash screen theme that you provide in the manifest
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<!-- Set the splash screen background, animated icon, and animation duration. -->
<item name="windowSplashScreenBackground">#color/...</item>
<!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
animated drawable. One of these is required. -->
<item name="windowSplashScreenAnimatedIcon">#drawable/...</item>
<!-- Required for animated icons -->
<item name="windowSplashScreenAnimationDuration">200</item>
<!-- Set the theme of the Activity that directly follows your splash screen. -->
<!-- Required -->
<item name="postSplashScreenTheme">#style/Theme.App</item>
</style>
Set it up in your manifest:
<manifest>
<application android:theme="#style/Theme.App.Starting">
<!-- or -->
<activity android:theme="#style/Theme.App.Starting">
...

Categories

Resources