user2558256
user2558256

Reputation: 223

Switching activity layout according to the button pressed in previous activity

I am a beginner in java and android. I have a categories activity with two buttons & the game activity with two layouts.I want to let the game activity detect which button is pressed in categories and set its corresponding layout by this way:

    if (Case_A)
   setContentView(R.layout.layout1);

   else if (Case_B)
   setContentView(R.layout.layout2);

With what code should I replace "Case_A" and "Case_B" to detect the button press ?

Upvotes: 0

Views: 226

Answers (1)

codeMagic
codeMagic

Reputation: 44571

When you create the Intent you can pass a value to GameActivity

Intent i = new Intent(CategoryActivity.this, GameActivity.class);
i.putExtra("layout", "layout1");
startActivity(i);

then in your GameActivity in your onCreate()

Intent intent = getIntent();
String layout = intent.getStringExtra("layout");
if (layout.equals("layout1")
   setContentView(R.layout.layout1);
   else if (Case_B)
   setContentView(R.layout.layout2);

This is one way to just pass any value you want depending on which Button was pressed. You also could get the Button text, id, or whatever you want and pass that.

Upvotes: 1

Related Questions