You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "veronicatc (via GitHub)" <gi...@apache.org> on 2025/04/08 13:22:16 UTC

[D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

GitHub user veronicatc created a discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

When opening my cordova app from deep links, the Android splash screen icon doesn't show.

I found out this is by design in Android, and to have it show, I need to add this to the theme style:

`<item name="android:windowSplashScreenBehavior">icon_preferred</item>`

How can I add this to the themes.xml file that is dynamically created by cordova cli when I compile the app?

Android documentation [https://developer.android.com/develop/ui/views/launch/splash-screen#set-theme](https://developer.android.com/develop/ui/views/launch/splash-screen#set-theme)

Thanks!



GitHub link: https://github.com/apache/cordova/discussions/535

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc added a comment to the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

Thank you for your answer!

I have tried this. I get a warning that themes.xml doesn't exist and thus the config-file directive will be ignored. I am guessing that config-file gets applied before the themes.xml file gets dynamically created. 

Do I need to create an after prepare hook script to edit the contents of the themes.xml file, perhaps replacing the </style> tag with the <item> plus </style> ? I'm not sure how I would implement that script in node.js though. 

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12766271

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc edited a comment on the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

I could achieve this using an after_prepare script. 

1) Make sure these are present at the <widget> tag at config.xml:

`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"`

2) Add hook inside android platform section:

`<hook src="scripts/after_prepare_theme.js" type="after_prepare" />`

3) This is the content of after_prepare_theme.js:

`   #!/usr/bin/env node

    // This hook adds a row to themes.xml after it has been dynamically created, to show splash screen icon when app is opened from deep links

    var fileToReplace = "platforms/android/app/src/main/res/values/themes.xml";

    var fs = require('fs');
    var path = require('path');

    function replace_string_in_file(filename, to_replace, replace_with) {
       var data = fs.readFileSync(filename, 'utf8');

       var result = data.replace(new RegExp(to_replace, "g"), replace_with);
       fs.writeFileSync(filename, result, 'utf8');
       var data2 = fs.readFileSync(filename, 'utf8');
       console.log(data2);
   }

   module.exports = function(context) {
    // var rootdir = process.argv[2]; // old cordova version
    var rootdir = context.opts.projectRoot;

    var fullfilename = path.join(rootdir, fileToReplace);
    if (fs.existsSync(fullfilename)) {
        replace_string_in_file(fullfilename, "</style>", '<item name="android:windowSplashScreenBehavior" tools:targetApi="33">icon_preferred</item></style>');
    }
    }
`

Tested on a device with Android 13 and on an emulator with Android 12. On Android 12 it didn't show the icon, as expected, but didn't throw errors either. Since I need this mostly for devices with Android 13 and after, this is ok for me.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12794683

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "breautek (via GitHub)" <gi...@apache.org>.
GitHub user breautek added a comment to the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

Should be doable via the [config-file](https://cordova.apache.org/docs/en/12.x-2025.01/config_ref/index.html#config-file) directive.

Untested example:

```xml
<widget ... xmlns:android="http://schemas.android.com/apk/res/android">
   ...
   <platform name="android">
       ...
       <config-file target="themes.xml" parent="/resources/style">
           <item name="android:windowSplashScreenBehavior">icon_preferred</item>
       </config-file>
   </platform>
</widget
```

Couple of notes:

1. to use the `android` XML namespace, you need to add the `xmlns:android` attribute on the `widget` tag if you don't already have one.
2. the [windowSplashScreenBehaviour](https://developer.android.com/reference/android/R.attr#windowSplashScreenBehavior) setting is only honoured on API 33 or later. I'm not sure if it will cause issues on earlier devices.

Cordova should setup the files on the `prepare` command, so you can use `cordova prepare android` to test, then take a look at `/platforms/android/app/src/main/res/values/themes.xml` to make sure you're augmentation was taken. Prepare is implicitly ran on build & run as well.

Because this doesn't describe a bug I'm also going to transfer this to the main cordova repo as a discussion.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12764845

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc added a comment to the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

I could achieve this using an after_prepare script. 

1) Make sure these are present at the <widget> tag at config.xml:

`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"`

2) Add hook inside android platform section:

`<hook src="scripts/after_prepare_theme.js" type="after_prepare" />`

3) This is the content of after_prepare_theme.js:

`#!/usr/bin/env node

// This hook adds a row to themes.xml after it has been dynamically created, to show splash screen icon when app is opened from deep links

var fileToReplace = "platforms/android/app/src/main/res/values/themes.xml";

var fs = require('fs');
var path = require('path');

function replace_string_in_file(filename, to_replace, replace_with) {
    var data = fs.readFileSync(filename, 'utf8');

    var result = data.replace(new RegExp(to_replace, "g"), replace_with);
    fs.writeFileSync(filename, result, 'utf8');
    var data2 = fs.readFileSync(filename, 'utf8');
    console.log(data2);
}

module.exports = function(context) {
    // var rootdir = process.argv[2]; // old cordova version
    var rootdir = context.opts.projectRoot;

    var fullfilename = path.join(rootdir, fileToReplace);
    if (fs.existsSync(fullfilename)) {
        replace_string_in_file(fullfilename, "</style>", '<item name="android:windowSplashScreenBehavior" tools:targetApi="33">icon_preferred</item></style>');
    }
}`

Tested on a device with Android 13 and on an emulator with Android 12. On Android 12 it didn't show the icon, as expected, but didn't throw errors either. Since I need this mostly for devices with Android 13 and after, this is ok for me.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12794683

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc edited a comment on the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

I could achieve this using an after_prepare script. 

1) Make sure these are present at the <widget> tag at config.xml:

`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"`

2) Add hook inside android platform section:

`<hook src="scripts/after_prepare_theme.js" type="after_prepare" />`

3) This is the content of after_prepare_theme.js:

`   
    #!/usr/bin/env node

    // This hook adds a row to themes.xml after it has been dynamically created, to show splash screen icon when app is opened from deep links

    var fileToReplace = "platforms/android/app/src/main/res/values/themes.xml";

    var fs = require('fs');
    var path = require('path');

    function replace_string_in_file(filename, to_replace, replace_with) {
       var data = fs.readFileSync(filename, 'utf8');

       var result = data.replace(new RegExp(to_replace, "g"), replace_with);
       fs.writeFileSync(filename, result, 'utf8');
       var data2 = fs.readFileSync(filename, 'utf8');
       console.log(data2);
    }

    module.exports = function(context) {
       // var rootdir = process.argv[2]; // old cordova version
       var rootdir = context.opts.projectRoot;

       var fullfilename = path.join(rootdir, fileToReplace);
       if (fs.existsSync(fullfilename)) {
          replace_string_in_file(fullfilename, "</style>", '<item name="android:windowSplashScreenBehavior" tools:targetApi="33">icon_preferred</item></style>');
       }
    }
`

Tested on a device with Android 13 and on an emulator with Android 12. On Android 12 it didn't show the icon, as expected, but didn't throw errors either. Since I need this mostly for devices with Android 13 and after, this is ok for me.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12794683

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc edited a comment on the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

I could achieve this using an after_prepare script. 

1) Make sure these are present at the <widget> tag at config.xml:

`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"`

2) Add hook inside android platform section:

`<hook src="scripts/after_prepare_theme.js" type="after_prepare" />`

3) This is the content of after_prepare_theme.js:

`   
    #!/usr/bin/env node

    // This hook adds a row to themes.xml after it has been dynamically created, to show splash screen icon when app is opened from deep links

    var fileToReplace = "platforms/android/app/src/main/res/values/themes.xml";

    var fs = require('fs');
    var path = require('path');

    function replace_string_in_file(filename, to_replace, replace_with) {
       var data = fs.readFileSync(filename, 'utf8');

       var result = data.replace(new RegExp(to_replace, "g"), replace_with);
       fs.writeFileSync(filename, result, 'utf8');
       var data2 = fs.readFileSync(filename, 'utf8');
       console.log(data2);
    }

   module.exports = function(context) {
    // var rootdir = process.argv[2]; // old cordova version
    var rootdir = context.opts.projectRoot;

    var fullfilename = path.join(rootdir, fileToReplace);
    if (fs.existsSync(fullfilename)) {
        replace_string_in_file(fullfilename, "</style>", '<item name="android:windowSplashScreenBehavior" tools:targetApi="33">icon_preferred</item></style>');
    }
    }
`

Tested on a device with Android 13 and on an emulator with Android 12. On Android 12 it didn't show the icon, as expected, but didn't throw errors either. Since I need this mostly for devices with Android 13 and after, this is ok for me.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12794683

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc edited a comment on the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

I could achieve this using an after_prepare script. 

1) Make sure these are present at the <widget> tag at config.xml:

`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"`

2) Add hook inside android platform section:

`<hook src="scripts/after_prepare_theme.js" type="after_prepare" />`

3) This is the content of after_prepare_theme.js:

`   
    #!/usr/bin/env node

    // This hook adds a row to themes.xml after it has been dynamically created, to show splash screen icon when app is opened from deep links

    var fileToReplace = "platforms/android/app/src/main/res/values/themes.xml";

    var fs = require('fs');
    var path = require('path');

    function replace_string_in_file(filename, to_replace, replace_with, test_included) {
       var data = fs.readFileSync(filename, 'utf8');
       var doReplace=true;
       if(typeof test_included != 'undefined' && test_included != '' && data.includes(test_included)){
           doReplace=false;
       }
       if(doReplace){
          var result = data.replace(new RegExp(to_replace, "g"), replace_with);
          fs.writeFileSync(filename, result, 'utf8');
          var data2 = fs.readFileSync(filename, 'utf8');
       }
    }
    module.exports = function(context) {
       // var rootdir = process.argv[2]; // old cordova version
       var rootdir = context.opts.projectRoot;

       var fullfilename = path.join(rootdir, fileToReplace);
       if (fs.existsSync(fullfilename)) {
          replace_string_in_file(fullfilename, "</style>", '<item name="android:windowSplashScreenBehavior" tools:targetApi="33">icon_preferred</item></style>', 'windowSplashScreenBehavior');
       }
    }
`

Tested on a device with Android 13 and on an emulator with Android 12. On Android 12 it didn't show the icon, as expected, but didn't throw errors either. Since I need this mostly for devices with Android 13 and after, this is ok for me.

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12794683

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


Re: [D] Make splash screen icon show when opening the app from deep link by including item in themes.xml [cordova]

Posted by "veronicatc (via GitHub)" <gi...@apache.org>.
GitHub user veronicatc edited a comment on the discussion: Make splash screen icon show when opening the app from deep link by including item in themes.xml

Thank you for your answer!

I have tried this. I get a warning that themes.xml doesn't exist and thus the config-file directive will be ignored. I am guessing that config-file gets applied before the themes.xml file gets dynamically created. 

Do I need to create an after prepare hook script to edit the contents of the themes.xml file, perhaps replacing the </style> tag with the < item> plus </style> ? I'm not sure how I would implement that script in node.js though. 

GitHub link: https://github.com/apache/cordova/discussions/535#discussioncomment-12766271

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscribe@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org