Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 273
Bahu Avatar asked Oct 18 '25 03:10

Bahu


1 Answers

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" />
like image 155
jcesarmobile Avatar answered Oct 19 '25 19:10

jcesarmobile