Search code examples
androidandroid-fragmentsresource-id

How to programmatically get resource Id for android.R.id.content?


I'm trying to do this in a library project...

int containerViewId = getResources().getIdentifier("content", "android.R.id", getPackageName());

getFragmentManager().beginTransaction().replace(containerViewId, new SettingsFragment()).commit();

But I get this exception on the replace: Caused by: java.lang.IllegalArgumentException: Must use non-zero containerViewId

How do I get resource id for android.R.id.content programmatically?

Thanks.

Edit: I have solved it by using a named view, setting the id in the xml layout of the activity and using the following code to replace the root view...

int containerViewId = getResources().getIdentifier("rootView", "id", getPackageName());
getFragmentManager().beginTransaction().replace(containerViewId, new SettingsFragment()).commit();

Solution

  • I have solved it by using a named view, setting the id in the xml layout of the activity and using the following code to replace the root view...

    int containerViewId = getResources().getIdentifier("rootView", "id", getPackageName());
    getFragmentManager().beginTransaction().replace(containerViewId, new SettingsFragment()).commit();