Reputation: 751
I am trying to decide on the best approach for creating an app splash-screen while taking into consideration Google's latest recommendations on choosing a single Activity app whenever possible.
See here:
"The new approach is to use one-activity structure whenever possible."
and here:
Any good splash-screen approaches I have found have a dedicated Activity for the splash screen:
Has anyone else had any experience creating a splash screen in a single Activity app? Does the the single Activity recommendation include the splash-screen or is it a special case? Does anyone have any good examples or advice on this?
Cheers, Paul.
Upvotes: 18
Views: 4551
Reputation: 2985
The approach I use is the following:
First define a drawable for the background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/green"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
2. Define a new style to use in the splashScreen:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
3. Make your activity implement use the splash theme:
<activity
android:name=".MainActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4. In on create, before the super invocation and before the set content view set the default app theme:
override fun onCreate(savedInstanceState: Bundle) {
setTheme(android.R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
}
This approach is the one I've been using even with multiple Activities, since it follows the guidelines laid down by google: itshows the splash right away and doesn't stay longer than needed.
Upvotes: 26
Reputation: 1338
If you are using ConstraintLayout
in your layouts, you can use the Group
class of Android to group multiple views. Please refer to the following link for more information.
https://developer.android.com/reference/android/support/constraint/Group
This class controls the visibility of a set of referenced widgets. Widgets are referenced by being added to a comma separated list of ids, e.g:
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="button4,button9" />
FYI - Multiple groups can reference the same widgets -- in that case, the XML declaration order will define the final visibility state (the group declared last will have the last word).
Hope this helps you resolve the issue.
Upvotes: 0