2

Is there a way to pass a string to 'setContentView()'? ie: setContentView(OUTPUT). The reason is that I have a class that I use for several activities (a custom ListView Adapter) and I need to use a different layout for one activity and would like not have a duplicate class where the only change is the setContentView(). I know that XML needs to be compiled in the APK but I am thinking that the inflation of the layout does not need to be in the onCreate...? So, Can this be done using a variable? Or does anyone have another idea/way of doing this? Thnx.

*The Class I am using is HERE in the 'REVISED' code section.

3
  • 1
    i haven't tried it, but if you pass extras to the intent then do if extras indicate use layout1 {setContentview(layout1);} else {setcontentview(layout2);}? Commented May 11, 2011 at 2:11
  • @jkhouw1 That maybe a good idea - I'll give it a try 2morrow. Thnx Commented May 11, 2011 at 2:13
  • 1
    it works, i just tested. Commented May 11, 2011 at 2:29

4 Answers 4

2

when starting calling your activity give it extras. Then in your activity, test for those extras and call the appropriate setcontentview.

Sign up to request clarification or add additional context in comments.

Comments

1

setContentView() takes an integer paramter that is relevant to R.java, so, no you cannot pass a string to it.

Source: http://developer.android.com/reference/android/app/Activity.html

Comments

0

Use Resources.getIdentifier() to get resources by name.

Comments

0

You have to parametrize your activity some way:

  • create an abstract activity class which takes int layoutId as constructor parameter, extend your activities from this class and pass different layouts
  • pass layout id as parameter of starting Intent
  • etc

Comments