Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding drawable resource using Cordova

I want to add a drawable resource to my cordova project. I did add the icon and splashscreen items just fine. They get copied to my platform/android/res/drawable just fine. The problem is when I try to add another resource. How do I do that? I can't find anything on cordova rather than icon and splashscreen

<platform name="android">
<icon src="xx.png" density="ldpi"/>
<icon src="xx.png" density="mdpi"/>
<icon src="xx.png" density="hdpi"/>
<icon src="xx.png" density="xhdpi"/>
<icon src="xx.png" density="xxhdpi"/>
<icon src="xx.png" density="xxxhdpi"/>
<splash src="xx.png" density="ldpi"/>
<splash src="xx.png" density="mdpi"/>
<splash src="xx.png" density="hdpi"/>
<splash src="xx.png" density="xhdpi"/>
<splash src="xx.png" density="xxhdpi"/>
<splash src="xx.png" density="xxxhdpi"/>

Can somebody help there? I don't see any option in the config.xml to add other drawable resources.

like image 792
Jose Granja Avatar asked Feb 19 '16 17:02

Jose Granja


People also ask

What is drawable resource?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

How do you insert a picture into a drawable resource?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.

Does Cordova need Android studio?

Cordova-Android projects can be opened in Android Studio. This can be useful if you wish to use Android Studio's built in Android debugging and profiling tools or if you are developing Android plugins. Note: When opening your project in Android Studio, it is recommended to NOT edit the code within the IDE.


2 Answers

EDIT:

Since Cordova CLI 7.x.x, when using cordova-android 6.2.x or cordova-ios 4.4.x you can use the resource-file tag from config.xml as I explained for on my old answer for plugins. You have to put it inside the platform tag.

Example:

<platform name="android">
  <resource-file src="www/res/drawable-hdpi/yourImage.png" target="res/drawable-hdpi/yourImage.png" />
</platform>

src is where you have the image right now, could be in www as in my example or even in the root of the project if you don't want the file duplicated. target is the destination, it will be in yourProject/platforms/platform/target for the example it would be yourProject/platforms/android/res/drawable-hdpi/yourImage.png

OLD answer:

From the config.xml you can only add icons and launch images

If you want to add a resource you can create a plugin and use the resource-file tag

<resource-file src="src/android/res/drawable-hdpi/yourImage.png" target="res/drawable-hdpi/yourImage.png" />

Or you can use a hook that copies the image to wherever you want

like image 126
jcesarmobile Avatar answered Sep 28 '22 05:09

jcesarmobile


For the next people like me, here is my simple hook to do this, to put in hooks/before_build/020_copy_resources.js:

#!/usr/bin/env node

// Copy native resources
var rootdir = process.argv[2];
var exec = require('child_process').exec;

// Native resources to copy
var androidNativePath = 'native/android/';

// Android platform resource path
var androidResPath = 'platforms/android/res/';

function copyAndroidResources() {
  exec('cp -Rf ' + androidNativePath + '* ' + androidResPath);
  process.stdout.write('Copied android native resources');
}

if (rootdir) {
  copyAndroidResources();
}

Just put your android resources in native/android/ then and you're good to go.

like image 34
sinedied Avatar answered Sep 28 '22 06:09

sinedied