0

Actually, 2 steps: creating themes, and creating condition (conditional method in activity)

  1. Creating new Splash Screen API implementation https://developer.android.com/develop/ui/views/launch/splash-screen

  2. and editing themes:

a) Main Splash Theme

This is a main Splash Theme (this one is for the devices with less than 12 (sdk 31) Android!)

Why is it like so? For the simplicity of the condition in the method in activity, so that we do not have to put two conditions: because 'if your_android_version is less than 12' is not quite a neat condition (you can try it if you do not believe…)

So, this will be our ‘parent’ theme:

style name="ThemeAppStarting" parent="Theme.SplashScreen"

(Setting the splash screen background, animated icon, and animation duration.)

Here - the background is white:

item name="windowSplashScreenBackground">#ffffff</item>

Note! The next property (drawable item) will be extended in our ‘subtheme’! In this theme we cannot omit a drawable setting, as the splash icon might be (at least as of today, Feb. 19) distorted or partially visible… (that certainly depends on your drawable too). So we put either the same drawable (meaning, the app’s icon, for example), but (Attention!) wrapped in an inset* with 'larger numbers' (‘83’), like so:

*//taken from here//>>> New Splash screen is shown cut in a cyrcle shape

<inset xmlns:android="http://schemas.android.com/apk/res/android"
                   android:drawable="@mipmap/ic_launcher"
                   android:insetLeft="83dp"
                   android:insetRight="83dp"
                   android:insetTop="83dp"
                   android:insetBottom="83dp"/>

//to create the inset right click on the ‘drawable’ folder > New > Drawable Resource File

Or, we can use any other drawable, but again (certainly, depending on the drawable’s properties) you might want to wrap it into an (separate) inset-->

item name="windowSplashScreenAnimatedIcon" type="drawable">@drawable/splash_inset_less_than_12_andr</item

Then, post splash screen(s) theme, that is required:

item name="postSplashScreenTheme">@style/Theme.MyApplicationHCLTest</item>

   </style>

b) Extended from Main Splash Theme

This theme is an extension theme (‘subtheme’) of the main them, with only one item (if you do not want to use windowSplashScreenBrandingImage):

style name="SplashThemeAndroid12Plus" parent="ThemeAppStarting"

Note!!! This one can be either with or without a drawable item! If it is without item, Android (since ?(do not remember) version)uses the application icon as a splash screen! If we define this item here (that is, give the link to a drawable resource), we, thus, can use any other drawable here!

Additionally, we can set the size of the icon on the device(s) screen(s) with the help of 'inset'*! *New Splash screen is shown cut in a cyrcle shape For that we should create a drawable resource (see above) and 'play' with the inset values (numbers): something like this:

inset xmlns:android="http://schemas.android.com/apk/res/android"
                   android:drawable="@mipmap/your_ic_launcher"
                   android:insetLeft="35dp"
                   android:insetRight="35dp"
                   android:insetTop="35dp"
                   android:insetBottom="35dp

and then just putting it like this:

item name="windowSplashScreenAnimatedIcon" type="drawable">@drawable/splash_inset_andr_12</item>

"Optionally, you can use windowSplashScreenBrandingImage to set an image to be shown at the bottom of the splash screen. The design guidelines recommend against using a branding image." https://developer.android.com/develop/ui/views/launch/splash-screen

with the aspect ration of smth about 446x177 (mine is like this)

  item name="android:windowSplashScreenBrandingImage" tools:targetApi="s">@drawable/your_branded_image</item>

   </style>
  1. Create a method for conditional themes usage

The core of it is where the conditional method should be put! (Because I tried to run it from onCreate… unsuccessfully, and for a long time…) Although the splash screen is instantiated in onCreate method, before super.onCreate and setContentView -- the coditional method should be put in onResume!!! I actually found the solution here at Stackoverflow, by someone who was asking about it, and then answered his/her question himself, I’ll include the link when I find it.

   @Override
    protected void onResume() {
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentApiVersion >= android.os.Build.VERSION_CODES.S) //public static final S = 31 //Android 12
        {
//you can check if not null here… (see below)
getSplashScreen().setSplashScreenTheme(R.style.ThemeAppStartingV31Plus);
        }
        super.onResume();
    }

I was communicating with OpenAI (https://chat.openai.com/chat) for that, so here what I learned from it:

“The issue might be that the onCreate() method of your activity might be getting called before the splash screen is finished, which means that the getSplashScreen() method might be returning null when you try to access it. To solve this, you could try moving your conditional statement to the onResume() method instead.”

"Yes, onResume() is another good place to put the conditional statement because it will be called every time the activity comes to the foreground, such as when the app is resumed from the background or after the splash screen finishes.”

“Your updated code looks good and it should work correctly. Using getSplashScreen().setSplashScreenTheme() is the proper way to set the splash screen theme dynamically at runtime, and using the R.style.ThemeAppStartingV31Plus theme you defined for Android 12+ devices is the right way to conditionally apply the new splash screen theme.”

More from OpenAI:

“Here's an example (Java):

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (getSplashScreen() != null) {
                getSplashScreen().setSplashScreenTheme(R.style.ThemeAppStartingV31Plus);
            }
        }
    }
}

By moving your conditional statement to the onResume() method, you ensure that the splash screen has finished before trying to access it.”

More from OpenAI:

“However, in this case, we can put the conditional statement in the setContentView() method, which is called inside onCreate().

Here's an example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 



    // check SDK version and set appropriate theme
   

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            setTheme(R.style.Theme_MyApp_V31);
        }
        
        setContentView(R.layout.activity_main);
    }

In this example, the setTheme() method is used to set the appropriate theme before calling setContentView(). The theme is set conditionally based on the device's SDK version using an if statement…”

More from OpenAI (starting with my question)

Me:

Ah, I see! Do you think it's (the advice above) a better alternative to putting the condition like this (in onResume):

 @Override
    protected void onResume() {
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentApiVersion >= android.os.Build.VERSION_CODES.S) //public static final S = 31 //Android 12
        {
getSplashScreen().setSplashScreenTheme(R.style.ThemeAppStartingV31Plus);
        }
        super.onResume();
    }

OpenAI:

“Yes, onResume() is another good place to put the conditional statement because it will be called every time the activity comes to the foreground, such as when the app is resumed from the background or after the splash screen finishes. Your updated code looks good and it should work correctly. Using getSplashScreen().setSplashScreenTheme() is the proper way to set the splash screen theme dynamically at runtime, and using the R.style.ThemeAppStartingV31Plus theme you defined for Android 12+ devices is the right way to conditionally apply the new splash screen theme. Just keep in mind that onResume() is called every time your activity comes into the foreground, so if your activity is paused and then resumed, the condition will be checked again. If the condition is expensive to evaluate or has side effects, you might want to consider putting it elsewhere. But if it's a simple check like the SDK version, it should be fine in onResume().”

I hope someone will benefit from this! Here is also a link to set the time the icon (non-animated) is on screen, if you like it: Android 12 Splash Screen API - Increasing SplashScreen Duration

0 Answers0