Accessing custom layout resources from the cordova plugin

I am trying to use LayoutInflater from my cordova plugin to replace the toast layout (adding an image to it), the problem is R.layout.custom_toast (to get the custom_toast.xml file) gives the error "error: cannot find symbol" and not works. Where should I put my custom_toast.xml file in the plugin folder structure to access it from my plugin java code? is there any magic to make this work? why is R not working?

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast_layout));

      

thank you very much in advance,

+3


source to share


1 answer


Since the plugin is not an activity (do not extend an activity) you cannot directly use R. What can you do instead of doing

R.layout.name_of_file

      

do the following:

Application app=cordova.getActivity().getApplication();
String package_name = app.getPackageName();
Resources resources = app.getResources();
int ic = resources.getIdentifier("name_of_file", "layout", package_name);

      



Thus, int ic will have the desired "filename" index, which is stored in R. Thus, instead of using R.layout.name_of_file, it is simple to use ic.

You also need to import these packages:

import android.app.Application;
import android.content.res.Resources;

      

+3


source







All Articles