SplashScreen approach defined in xml (without activity) does not appear - android

I have defined splash screen like many other without external activity, but it doesn't works for me, I don't understand why.
Is there any limitations for images? Also tried for many other images
This is my splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash_logo" />
</item>
</layer-list>
Here preview for splash.xml file in android studio looks like perfectly.
Than I have created style like that
<style name="AppTheme.Launcher">
<item name="android:windowBackground">#drawable/splash</item>
</style>
and assigned it to activity theme inside manifest
<activity
android:theme="#style/AppTheme.Launcher"
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
this is my activity's
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
when activity is loaded blank white screen is shown instead of image, maybe for a one second. I searched this approach and seems that it is the same what others were done. Don't understand what's wrong
EDIT:
I have tested it for another device OS 5.0 and it works, but for 6.0 doesn't

EDIT
Remove this from your activity
setTheme(R.style.AppTheme)
Try this
Style.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_background</item>
</style>
splash_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher" />
</item>
</layer-list>
SplashActivity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
// close splash activity
finish();
}
}
Manifest
<activity android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

This should be a comment. But i cannot put a comment. I have faced the same problem. Please check your size of image. Try to compress the image and try again it may help you. Please do let me know.

Related

How do I make the picture on the splash screen come after 1 second

I coded splash screen in my project but ı want to splash screen image come 1 second after splash screen starts. Can you create a sample code block about this or provide a source?
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
if (UiHelper.hasInternetConnection(this)) {
loadSplashScreen()
} else {
UiHelper.customErrorDialog(this, "Connection failed")
}
}
private fun loadSplashScreen() {
Handler().postDelayed({
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 3000)
}
I writed warning messages on componion object. Inside the companion object ui helper class.
Following the best practice to make a spash screen:
0. Inside res/drawable folder create a file "launch_screen.xml"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimaryDark" />
<item>
<bitmap
android:gravity="center"
android:src="#drawable/my_splash_image" /> <---- replace with your spash image
</item>
</layer-list>
1. Insert in your Manifest.xml this line android:theme="#style/AppTheme" like follow and inside first activity: android:theme="#style/AppTheme.Launcher"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.myapp">
<application
android:name=".MyApp"
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=".MainActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppTheme.Launcher">
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2. Open styles.xml from res/values folder and paste the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<style name="AppTheme.Launcher">
<item name="android:windowBackground">#drawable/launch_screen</item>
</style>
</resources>
And all is done :-)

How to change the base color of an application

When I launch my application, the screen goes white before displaying my Splash Screen
I would like to remove if possible or put the background the same color as my Splash Screen
But I do not know how, Can someone help me
Thank you
You need to remove your splash screen code as the white screen is itself a splash, provided by android. You just need to change it according to your need.
You first need to create a drawable with name splash_screen.xml and put this code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/black"/>
<item
android:drawable="#mipmap/ic_launcher_round"
android:gravity="center"/>
</layer-list>
after that you need to create a activity which launches your MainActivity
Something like this
class OnBoardingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DashboardActivity.launch(this)
finish()
}
}
then you need to overwrite the default splash screen with your styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
after that last step is to add this theme in your manifest.
the activity which launches the app will have this style
<activity
android:name=".ui.onboarding.OnBoardingActivity"
android:label="#string/app_name"
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>
you could use separate activity for your splash screen
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:background="#dddddd"
tools:context=".SplashScreen">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_icon" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Splash Screen"
android:textSize="25sp" />
Class
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(getApplicationContext(),MainActivity.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}
}, 2000);
}
}
Manifest
( Launch Activity as SplashActivity )
<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/Theme.SplashtoMain">
<activity android:name=".MainActivity"/>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

How to show android splash screen in landscape mode?

I am working on an android app and would like to show a splash screen in landscape mode when the system loads the app.
I made a small test app to reproduce the problem and saw that it actually works in portrait mode, but when I add this line android:screenOrientation="sensorLandscape" to my manifest it suddenly doesn't work anymore. It doesn't show any splash screen as if the property windowPreviewDisabled was set to true.
Has anyone an idea what I am doing wrong?
My manifest:
<activity android:name=".MainActivity" android:theme="#style/AppTheme.Launcher" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.
</intent-filter>
</activity>
style xml:
<style name="AppTheme.Launcher" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#layout/launch_screen</item>
</style>
launch screen xml (which is inside layout-land folder):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="#android:color/black"/>
<item>
<bitmap
android:src="#drawable/logo2"
android:gravity="center"/>
</item>
</layer-list>
Maybe you could add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
in your activity after the setContentView method.

How use centerCrop in SplashActivity

In my SlashActivity I use only AndroidManifest and style as advised here https://www.bignerdranch.com/blog/splash-screens-the-right-way/.
And in SplashActivity I want use splash.jpg, but this picture changing to screen size.
How I can use centerCrop in my style file or are there other solutions?
My drawble:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/colorMaterialWhite"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash" />
</item>
</layer-list>
AndroidManifest:
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashActivity:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, NavigationActivity.class);
startActivity(intent);
finish();
}
}
Style:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
You might want to add padding to your splash image.
Inside the item tag add android:top, android:bottom, android:right, android:left
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/colorMaterialWhite"/>
<item
android:top="200dp"
android:bottom="200dp"
android:left="200dp"
android:right="200dp">
<bitmap
android:gravity="center"
android:src="#drawable/splash" />
</item>
</layer-list>
I have also followed this blog while creating my Splash screen. Generally, the splash screen has a background color and the logo of your app and you can set it as a background. So, you should not require to centerCrop the image in Splash screen. In case that need is there, I would strongly recommend to edit the image using any editor instead of programmatically doing it.

Animation in Splash Screen

I use this splash screen for my app. It works fine, but I want to use animation. How can I use animation without layouts?
This my java file:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, NavigationActivity.class);
startActivity(intent);
finish();
}
}
My xml for theme:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/colorMaterialWhite"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ico" />
</item>
</layer-list>
Style:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
AndroidManifest:
<activity android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I want rotate ico while app loading. Thank you.
Is it even possible?

Categories

Resources