Bahu
Bahu

Reputation: 1596

How to set images in Cordova plugin

I'm converting android native code into Cordova plugin. In native i'm using following code to set the image in Floating Action Button

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record, getTheme()));
    } else {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record));
    } 

My doubt is, In android we will use R.drawable.XXXXXX for setting the image. Like wise how we can set the image in cordova plugin and where we have to store the images (Like drawable folders in android native)

Upvotes: 4

Views: 1490

Answers (1)

jcesarmobile
jcesarmobile

Reputation: 53301

Example from InAppBrowser plugin

Resources activityRes = cordova.getActivity().getResources();
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
Drawable backIcon = activityRes.getDrawable(backResId);
if (Build.VERSION.SDK_INT >= 16)
    back.setBackground(null);
else
    back.setBackgroundDrawable(null);
back.setImageDrawable(backIcon);

The images are copied using the resource-file tag in plugin.xml

<resource-file src="src/android/res/drawable-hdpi/ic_action_previous_item.png" target="res/drawable-hdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-mdpi/ic_action_previous_item.png" target="res/drawable-mdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xhdpi/ic_action_previous_item.png" target="res/drawable-xhdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_previous_item.png" target="res/drawable-xxhdpi/ic_action_previous_item.png" />

Upvotes: 4

Related Questions