I had a very similar problem before where i wanted to show random xml layouts. I’ve done that – with lots of help by Ben Williams – with a class named DisplayRandomPage.class and I had a main.xml layout with four buttons.
That’s the code of the Main.class:
switch (view.getId()) {
case R.id.first_button:
startActivity(new Intent(this, FirstPage.class));
break;
case R.id.second_button:
startActivity(new Intent(this, SecondPage.class));
break;
case R.id.third_button:
startActivity(new Intent(this, ThirdPage.class));
break;
case R.id.random_button:
Intent intent = new Intent(this, DisplayRandomPage.class);
startActivity(intent);
and this is in the DisplayRandomPage.class:
public class DisplayRandomPage extends Activity {
private Integer [] mLinearLayoutIds = {
R.layout.one
R.layout.two,
R.layout.three
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new java.util.Random();
int rand = random.nextInt(3);
setContentView(mLinearLayoutIds[rand]);
}
}
What i’d like to do is creating a DisplaySpecificPage.class. Above I’ve shown my main.class with the switch-case-clause. So when i click on the first button, it will start the FirstPage.class, clicking the second, it will start SecondPage.class, and so on. So for each xml-file i have to create a new java-class although the different java-classes do all the same. Only the xml-files are different. So i’d like to put something like this:
pseudo-code:
case R.id.first_button:
startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page;
break;
how do i pass the ID from the layout (R.layout.first_page) on?
You should change your
switchstatement toThis way you’d start the same activity no matter which button would be pressed, and inside the
DisplaySpecificPageactivity’sonCreatemethod you should set the content to this passed layout:The code above passes an extra parameter to the intent when starting the
DisplaySpecificPageactivity, with the name: “mylayout”.Inside the
DisplaySpecificPageclass’onCreatemethod you just retrieve this extra parameter using thegetIntExtramethod of the passed intent (getIntent()will return it for you), and you set the content view of thisDisplaySpecificPageactivity to the passed layout bysetContentView(layout).You should make sure though, to always pass a valid layout identifier to that intent, otherwise you’ll get exception when trying to inflate it (so
randomNumbershould be selected properly).Update
With adding extras to the Intent you can parametrize your activities.
So using
intent.putExtra("paramName", paramValue)you’ll pass theparamValuevalue on the name ofparamNameto the activity you start by this intent.You want to start the
DisplaySpecificPageactivity, but want it to have different layout based on which button you click.So you create an intent:
Before starting the activity by calling
startActivity(intent), you have to put this extra information inside theintent, so theDisplaySpecificPageactivity to know which layout it should set as its content view:So if you pressed the second button, you want the layout of your new activity to be the one defined by the
two.xmlinside yourres/layoutfolder. It is referenced by asR.layout.two(which is astatic intvalue).This line puts the layout’s value as an extra into the
intentobject, and now you can start the activity, the layout reference will be passed to it.The “mylayout” is a name you choose for your parameter. It can be anything (valid), it will be used inside the
DisplaySpecificPageactivity to retrieve the layout’s reference. It is retrieved by this name:The
getIntExtramethod gets the integer value from theintentwhich has the name “mylayout”, and if it’s not found, then it will return R.layout.one.If you want to handle this case (when no parameter with name
mylayoutis set), you can write