user1169079
user1169079

Reputation: 3083

How to go to next Activity in Android

I have three views main1.xml, main2.xml , main3.xml. In main.xml I'm putting three buttons. When the user clicks on first button I want it to show main1.xml, if second button then main2.xml etc.. I am done with the main.xml files.

How to do I change the view?

Upvotes: 3

Views: 9877

Answers (6)

H.Fa8
H.Fa8

Reputation: 318

use Intent in startActivity .

startActivity(new Intent(FirstActivity.this, secondActivity.class);

Upvotes: 0

Prateek Sharma
Prateek Sharma

Reputation: 19

Intent intent = new Intent(getApplicationContext(), QuizSectionActivity.class);
startActivity(intent);

Upvotes: 0

Unicusand
Unicusand

Reputation: 65

Call method setcontentView(r.layout.main1),setcontentView(r.layout.main2),setcontentView(r.layout.main3) from click event of you three button respectively.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

I think this question is not about to go to next activity, but to flip sub-layouts to display on the screen.

I am sure the solution which i am going to suggest you, will suites your requirement exactly.

The solution is to implement ViewFlipper widget in your xml layout. Because the main purpose of ViewFlipper is to flip Views whenever required.

As you are having three sub-layouts main1.xml, main2.xml , main3.xml in main.xml layout, you just need include this sub-layouts as:

<ViewFlipper>
    <LinearLayout/>        <!-- main1.xml -->
    <LinearLayout/>        <!-- main2.xml -->  
    <LinearLayout/>        <!-- main3.xml -->  
</ViewFlipper>

Now, in your Activity, you have to find this ViewFlipper by their id by using findViewById() method.

Now, to show a particular layout on the click event of its related button, you have to call myViewFlipper.setDisplayedChild().

For example: On clicking of 1st button, you just need to write:

myViewFlipper.setDisplayedChild(0);

You can refer this example: View Flipper Example

Upvotes: 2

Lucifer
Lucifer

Reputation: 29632

Its very simple, you just need to use Intent

Intent intent = new Intent(this, ActivityTwo.class);
this.startActivity ( intent );

Upvotes: 16

Andro Selva
Andro Selva

Reputation: 54322

Use Intents ,

in your main activity create a button and inside its onClick do this

 button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent intent=new  Intent(mainActivity.this,nextActivity.class);
                 startActivity(intent);


                }
            });

And in your next Activity's onCreate() , use setContentView(//ur xml);

Upvotes: 1

Related Questions