Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4593478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:41:24+00:00 2026-05-21T22:41:24+00:00

I had a very similar problem before where i wanted to show random xml

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:41:25+00:00Added an answer on May 21, 2026 at 10:41 pm

    You should change your switch statement to

    final Intent intent = new Intent(this, DisplaySpecificPage.class);
    switch (view.getId()) {
        case R.id.first_button:
            intent.putExtra("mylayout", R.layout.one);
            break;
        case R.id.second_button:
            intent.putExtra("mylayout", R.layout.two);
            break;
        case R.id.third_button:
            intent.putExtra("mylayout", R.layout.three);
            break;
        case R.id.random_button:
            intent.putExtra("mylayout", randomNumber);
            startActivity(intent);
    }
    startActivity(intent);
    

    This way you’d start the same activity no matter which button would be pressed, and inside the DisplaySpecificPage activity’s onCreate method you should set the content to this passed layout:

    final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
    setContentView(layout);
    

    The code above passes an extra parameter to the intent when starting the DisplaySpecificPage activity, with the name: “mylayout”.

    Inside the DisplaySpecificPage class’ onCreate method you just retrieve this extra parameter using the getIntExtra method of the passed intent (getIntent() will return it for you), and you set the content view of this DisplaySpecificPage activity to the passed layout by setContentView(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 randomNumber should be selected properly).

    Update
    With adding extras to the Intent you can parametrize your activities.

    So using intent.putExtra("paramName", paramValue) you’ll pass the paramValue value on the name of paramName to the activity you start by this intent.

    You want to start the DisplaySpecificPage activity, but want it to have different layout based on which button you click.
    So you create an intent:

    final Intent intent = new Intent(this, DisplaySpecificPage.class);
    

    Before starting the activity by calling startActivity(intent), you have to put this extra information inside the intent, so the DisplaySpecificPage activity 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.xml inside your res/layout folder. It is referenced by as R.layout.two (which is a static int value).

    intent.putExtra("mylayout", R.layout.two);
    

    This line puts the layout’s value as an extra into the intent object, 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 DisplaySpecificPage activity to retrieve the layout’s reference. It is retrieved by this name:

    final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
    

    The getIntExtra method gets the integer value from the intent which 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 mylayout is set), you can write

    final int layout = getIntent().getIntExtra("mylayout", -1);
    if (layout < 0) 
    {
        //TODO: no layout reference was passed
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

StackOverflow user jolson had a very nice piece of code that exemplifies how one
I'm working on an application that has a view hierarchy that is very similar
The company I work for has historically had very little process as far as
I have some questions about customers about NF mode for DB2. Google had very
I am very new to creating webservers - and I have had several goes
Recently I had to do some very processing heavy stuff with data stored in
We have had issues with Mootools not being very backward compatible specifically in the
I'm looking to build a library that needs to be very careful about memory
I have a very simple VS2005 deployment project that aims to install for all
this question probably wont be explained very well and that's because I don't really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.