2

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();
CaptAmericode
  • 83
  • 1
  • 4
  • I can access most all resource names programmatically. That isn't the issue. It is this particular resource id (android.R.id.content) that is returning zero. I'd like to know the proper way to get the root content view programmatically (android.R.id.content) to replace it, via the fragment manager. – CaptAmericode Feb 18 '14 at 00:23

3 Answers3

2
int containerViewId = findViewById(android.R.id.content) 

should work. You have to make sure you are using the Activity's context though. You can also use Activity.getWindow().getDecorView() to get the top view.

David C Adams
  • 1,953
  • 12
  • 12
  • Both of these suggestions return a View, however I need the resource id of the view. – CaptAmericode Feb 18 '14 at 01:18
  • android.R.id.content is the id of the view. If it's returning zero, are you sure you are trying to get this from the Activity context, or in the fragment? – David C Adams Feb 18 '14 at 02:00
  • 1
    It's from an Activity context, however it is from within an embedded library project. I think that's why it's returning zero. The resources are merged. I've decided to not use android.R.id.content, but simply to replace the root view of my layout by setting the id for it. – CaptAmericode Feb 20 '14 at 05:34
0
int containerViewId = view.getId();

then use id as you want but you just need to get view as parameter or get from somewhere else

vfongmala
  • 185
  • 1
  • 2
  • 11
0

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();
CaptAmericode
  • 83
  • 1
  • 4