Reputation: 235
At my company we are using Ionic Framework and Cordova to create our mobile app, and upon starting the design of the app, we encountered an issue with the Android theme and how to set it without touching the AndroidManifest generated by Ionic build command.
Everywhere I look it is recommended to implement any customization from the config.xml file and never touch the AndroidManifest, but I cant seem to find any methods regarding the Android theme.
My question to you now: Is there a way to set the android theme for the application, for example Holo Theme, from the Config.xml without changing the AndroidManifest.xml generated?
Upvotes: 6
Views: 12561
Reputation: 298
You can use Header Color plugin:
Install the plugin:
$ ionic cordova plugin add cordova-plugin-headercolor
$ npm install --save @ionic-native/header-color
Add configuration to config.xml
<preference name="HeaderColor" value="#becb29" />
https://ionicframework.com/docs/native/header-color/
Upvotes: 1
Reputation: 1814
You can do this now without any third party plugin since 6.3.0. Just add this to the config.xml
<platform name="android">
<edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:label='@string/activity_name']" mode="merge">
<activity android:theme="@android:style/Theme.Translucent"></activity>
</edit-config>
</platform>
and for me it was also neccessary to add 'xmlns:android="http://schemas.android.com/apk/res/android" ' to the widget tag
<widget id="de.bestellkind.restaurant" version="1.0.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
Upvotes: 14
Reputation: 734
I know I'm late, but cordova-custom-config
plugin made just to "update platform configuration files based on preferences and config-file data defined in config.xml."
for example:
install the cordova-custom-config plugin: cordova plugin add cordova-custom-config --save
Config.xml: <preference name="android-manifest/application/activity/@android:theme" value="@android:style/Theme.Holo"/>
This will add the attribute "android:theme" to your AndroidManfiset --> application --> activity with the value: @android:style/Theme.Holo
.
Upvotes: 5
Reputation: 1049
To avoid touching the platforms directory, you could use a cordova hook. I am pretty terrible at node, but here is something that should do the trick. First npm install elementtree
then create a sub folder after_prepare
in the hooks folder. From there stick this code into a javascript file and change YourTheme.
Honestly, this is some pretty gross code, but should give you the idea.
#!/usr/bin/env node
var fs = require( "fs" );
var et = require('elementtree');
var rootdir = process.argv[2];
console.log(rootdir);
fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'r+',
function (err, fd) {
if (err) {
exitError(err);
}
fs.stat(rootdir + '/platforms/android/AndroidManifest.xml', getStats);
function getStats(error, stats) {
if (error) {
exitError(error);
}
var buffer = new Buffer(stats.size);
fs.read(fd, buffer, 0, buffer.length, null, fileRead);
}
function fileRead(error, bytes, buf) {
var data = buf.toString("utf8", 0, buf.length);
var androidXML = et.parse(data);
var root = androidXML.getroot();
var activityTag = root.find("application/activity");
activityTag.attrib["android:theme"] = "@style/YourTheme";
var outputBuffer = new Buffer(et.tostring(root), "utf-8");
console.log(outputBuffer.toString());
fs.closeSync(fd);
fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'w', writeFile);
function writeFile(error, fd) {
if (error) {
exitError(error);
}
fs.write(fd, outputBuffer, 0, outputBuffer.length, 0, function( errw, written, str) {
if (errw) {
exitError(errw);
}
console.log('file written');
fs.close(fd);
});
}
}
});
function exitError(error) {
console.log(error);
process.exit(0);
}
Upvotes: 4