Launcher activity setContentView delay - android

I created a simple app with Splash screen which is shown for 1 second. Splash screen is almost red. But I noticed that when I launch my app at first for ~0.3 sec appears white screen and after that appears my Splash screen. Is it possible to remove this white screen or make it predefined red? I testes it on nexus 4 with android 5.0, and my Splash activity have quite simple implementation, and nothing can delay onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
waitTask = new WaitTask();
waitTask.execute();
}
private class WaitTask extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
TimeUnit.MILLISECONDS.sleep(SPLASH_DURATION);
} catch (InterruptedException e) {
// Do nothing
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
I have noticed that almost all apps show it first screen with some delay and this white screen, but in some app this screen is back. Is there any way to control it?

You can define the style of the activity and set it in the Manifest. This way, when the activity runs, it will use its pre defined style (in which you will have the background or windowBackground set to red) and within that 0.3 seconds, the windows will still be red.
Define the colour:
<resources>
<color name="background">#FF0000</color>
</resources>
Then define the style:
<resources>
<style name="MyLaunchActivityTheme" parent="#android:style/Theme.Light">
<item name="android:windowBackground">#color/background</item>
</style>
</resources>
Then set the style for the activity in the manifest:
<activity
android:name=".MyActivity"
android:theme="#style/MyLaunchActivityTheme"/>

The ans Sam gave is perfect but to extend it - let you want to have your custom splash screen image visible for the whole time, If we apply color to the window then there will be a switch from color to the actual slash screen. The ans which i propose dont require setContentView but only manage splash screen through theme.
We can't set the theme in java as in my case the control came to onCreate of my splash activity very late. Till that time black screen keep visible.
This gave me clue that window has to be managed from theme which we specify in manifest.
There is the manifest i have:
<activity
android:name=".main.activities.SplashScreen"
android:theme="#style/Splash"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now The theme i created is as follows:
<style name="Splash" parent="#style/Theme.AppCompat.Light">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">#color/green_09</item>
<item name="colorPrimary">#color/green_09</item>
<item name="windowActionBar">false</item>
</style>
Splash in the drawable resource which contains a bitmap resource, I have to tweak a bit to make it look perfect not stretched and in center:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:gravity="fill"
android:src="#drawable/splash_screen" />

Related

How do I make my splash screen load faster like Snapchat and others

My splash screen on Android Studio loads a grey background before it goes to my splash screen and then the main activity.
How do I make the grey loader the splash screen?
Maybe you have done lots of programming in oncreate() method. keep it as minimal as possible. check this useful tutorial for splash screen: http://tusharpatil.com/blog/how-to-implement-splash-screen-in-android-java/
Your splash activity is most likely quite heavy. You can add a very minimal activity that just forwards on to your larger splash activity
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
}
}
Use that activity as the launching intent in AndroidManifest.xml
<activity
android:name=".view.splash.Splash"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Also, create a theme in styles.xml for the splash forwarding activity.
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimaryDark">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorAccent</item>
</style>
Here I just used the accent color for a background that matches the following splash activity for easier transition. It would avoid the grey in your case. There are other alternatives like Tushars solution, but this works for my use case
Hope this helps

Android splash screen is shrunk

I have trouble with android splash screen.
I made splashscreen like these
/platform/android/res/drawable-hdpi/background.9.png
/platform/android/res/drawable-mdpi/background.9.png
/platform/android/res/drawable-xhdpi/background.9.png
/platform/android/res/drawable-xxhpi/background.9.png
/platform/android/res/drawable-xxxhdpi/background.9.png
These are made by ticon automatically.
and have confirmed these looks OK(correct background.9 below).
However, when I open appication (on Nexus7 2013),splashscreen looks much smaller.
I mean background.9.png are centered and there are white blank around image.
correct background.9
shown splashscreen
How can I adjust it to the screen???
To adjust it to the screen, you just have to resize it accordingly and then create a drawable file like this for each screen size (this exemple will display a gradient background with an image in the center)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2b3143"
android:centerColor="#37465e"
android:endColor="#55647c"
android:angle="135"/>
</shape>
</item>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/myCuteLogo"/>
</item>
You then create a style and set the property android:windowBackground with the drawable you created:
<style name="SplashScreenStyle" parent="myAppTheme">
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
Then create a SplashActivity that will display the Splash screen:
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, Activity_a_Main.class);
startActivity(intent);
finish();
}}
Lastly in your manifest set the SplashActivity as the Lancher activity and set its theme to the style you created:
<activity android:name=".Activities.SplashScreen"
android:theme="#style/SplashScreenStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope this helps you
NOTE: If you set the style you created as the whole application's theme, it will show your drawable in weird places, like in the background of a soft keyboard

Override activity's background that was set by theme

I currently have an Activity for my splash screen. The following is the way I apply a theme to the activity:
<application
...
<activity
android:name=".activities.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
SplashTheme is defined as follows:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">#drawable/splash_background</item>
</style>
splash_backround.xml is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/login_background"
android:tileMode="disabled" />
</item>
<item>
<bitmap
android:src="#drawable/login_siren"
android:tileMode="disabled"
android:gravity="bottom|center_horizontal" />
</item>
<item android:top="120dp">
<bitmap
android:src="#drawable/splash_welcome"
android:tileMode="disabled"
android:gravity="top|center_horizontal" />
</item>
I am not calling setContentView() in onCreate() of my SplashActivity class, so thing being displayed is what is set by the SplashTheme.
A situation has come up where I want to display an AlertDialog if something fails to load upon Application.onCreate(). The splash screen is displaying at the time I use this to build, create and show the AlertDialog. However, when I show the AlertDialog, it gets assigned the background from the Activity's theme. Even if I define a custom style for the AlertDialog (via android:alertDialogTheme or android:alertDialogStyle) that explicitly defines a different background, it gets overruled by the background defined by the activity. As a result, I've changed SplashTheme to the following:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_background</item>
</style>
This now releases control of the alert dialog background as I am not explicitly setting android:background in my Activity theme. However, the window background does not account for the status bar (i.e. the background applies underneath the status bar as well). As a result, I get a bit of jump when the splash screen transitions to the next activity that uses this same background in its layout (due to accounting for the status bar). So, this solution still isn't working as I'd like.
If I do use the original SplashTheme (where I define android:background), is there a way to override the theme's defined android:background for an AlertDialog?
It is not possible to change a theme of activity once it is applied.
Is there a way to override the theme's defined android:background for an AlertDialog?
AlertDialog has a constructor, which accepts themeResId as an input. From docs of AlertDialog (Context context, int themeResId):
Creates an alert dialog that uses an explicit theme resource.
Create a custom theme, overriding android:background attribute, then create AlertDialog using newly created theme:
Dialog dialog = new AlertDialog(context, R.style.my_dialog_theme);

How to create Android application without ActionBar using new theme in API9?

I need to create application wihtout actionbar. I will try to explain how I create application(maybe I am missing some steps).
1.I am intentionally not choosing "Holo Light with Dark Action Bar" option. I thought this make application without action bar.
2.Leaving next page as default:
3.Leaving next page as default:
4.On the next page, it becomes interesting. Blank Activity's description says that it creates a new blank activity an action bar. On Empty Activity's description: Creates a new empty activity. So I choose Empty activity
5.Leaving next page as default.
Result:As usually, application was created with ActionBar.
Tried this solution:getActionBar().hide();. In this solution, ActionBar is hiden after some milliseconds.
Tried to use android:theme="#android:style/Theme.NoTitleBar" in AndroidManifest.xml->application tag. But in this solution, application uses old theme.
My question: how to create Android application without ActionBar using new theme and leaving StatusBar enabled?
PS. I remember, about one year ago, in Eclipse, applications were created without ActionBar. Then we added it manually
Put this right after super.onCreate(savedInstanceState); in the onCreate method of your activity(s):
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Make sure you put this BEFORE this.setContentView(R.layout.activity_example); to avoid crashes
When your project is started you can use public activity extends Activity instead of using ActionBarActivity this will also remove the action bar or there is another way
<android:theme="#android:style/Theme.NoTitleBar">
or you can see these links they might help
How to hide action bar before activity is created, and then show it again?
Remove android default action bar
Refer this code and change as per your requirement:
Update :
styles.xml :
<style name="FullscreenTheme" parent="android:Theme.Holo">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:actionBarStyle">#style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>
<style name="FullscreenActionBarStyle" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#color/black_overlay</item>
</style>
These will remove actionbar and keeps your theme as it is .
Note : Here i have tested using Theme.Holo . you can use yours.
AndroidManifest.xml
<activity
android:name=".GridViewExample"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It gives you activity without ActionBar.
Hope this solves your problem.
You can do it programatically:
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Or you can do it via your AndroidManifest.xml file:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
EDIT :
If your app uses any other theme use corresponding theme name E.g. For White theme #android:style/Theme.Holo.Light.NoActionBar.Fullscreen

Android - Disable dummy starting window of application

I want to know how to achieve this effect.
My app (default everything), when launcher icon is clicked, right away display some kind of a empty dummy window, where nothing is happening and then loads layout into it.
"Heavier" apps like YouTube, Drive, Dropbox etc. when started, seem to wait after launch, without showing that dummy window and load right into ready layout.
Any idea how to do this or where should I look into?
Thanks
//EDIT: this has nothing to do with something like loading database, where I should display progressBar, imho this has to do with stuff before activity exists.
I suggest you don't disable the dummy loading window using:
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
because you could run into several problems. For example, if you try to animate an AlertDialog, enter animation will not work (personal experience).
The right solution is to set the theme of your application like to your main activity screen (same ActionBar style, same background color).
In my application after severals experiments i found the right solution for my needs.
(I have a main activity with blank background, no ActionBar, just full screen personal layout)
1 - styles.xml: add new style like this (remove ActionBar and set the same background color of my main activity)
<style name="LoadingTheme" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#color/white_smoke</item>
</style>
2 - AndroidManifest.xml: set my theme for main activity to "LoadingTheme"
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.company.appname.MainActivity"
android:label="#string/app_name"
android:theme="#style/LoadingTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
And finally i have full blank dummy loading window without ActionBar with soft MainActivity load and working animations.
Hope to be helpful.
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
but use with caution
This will create a custom preview window for your activity.
Create a style in styles.xml like
<style name="MyTheme" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:windowBackground">#color/myBackgroundColor</item>
</style>
In your Manifest file use the theme created above.
For eg:
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/MyTheme"
/>
In your activity change the background of the window back to null in onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
setContentView(R.layout.main);
}
You can call the setContentView method anywhere you'd like in your activity. Before calling this method it will display a blank screen. If you wanted a splash screen, you can call the setContentView method within the onCreate method to display the splash screen view. Then when you're ready to display the "main" view you can use a LayoutInflater to display it. I also suggest using a ProgressDialog to show the user that information is loading.
Load your resources before setting the contentview. There will be a dummy window until you would not set view using setContentView.
EDIT
To avoid this screen. Make there changes to your code.
1) Do not do any heavy work in activity onCreate method. Run a separate thread for it.
2) Set content view right after the super.onCreate() line
super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);
3) Apply following theme to your application.
Define theme in style
<resources>
<style name="Theme.MyTheme" parent="android:style/Theme" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/any_default_background</item>
</style>
</resources>
In your Manifest
<application
....
android:theme="#style/Theme.MyTheme"
....
>
Thats It.

Categories

Resources