I am currently doing this. It only works if you know the name of the activity package or the fragment from .apk that should provide the layout hierarchy (I call this external context). And you need to know the name of the layout you want to inflate (for example, R.layout.mylayout → "mylayout")
Context c = createPackageContext(foreignPackageName, Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY); //create foreign context int resId = c.getResources.getIdentifier(mylayoutName,"layout",foreignPackageName); LayoutInflater myInflater = LayoutInflater.from(c); //Inflater for foreign context View myLayout = myInflater.inflate(resId,null,false); //do not attach to a root view
source share