-1

I Have created 2 activities. The first one is named activity_main.xml and the other one layout.xml I have a button in the first one and I want thst on clicking it I reach layout.xml How do I do this?   This is the code for the button:

I am a beginner so it will be better if anyone can give me step by step instructions

K.A. Siddiqui
  • 49
  • 1
  • 8

3 Answers3

1

Use Intent to call the SecondActivity. If in your activity_main.xml there is an Button. Then provide onClickListnere on the Button to call the Second Activity.

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent inent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
           }
});
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
1

The button id from the activity_main.xml will be used to call the event for going from ActivityMain to SecondActivity, don't forget to initialise the button in onCraete.

Use this code(inside onCreate is preferred, not necessarily):

Button button = (Button) findViewById(R.id.button_id_from_activity_main.xml);
button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   startActivity(new Intent(your_activity_name.this, second_activity_to_go.class););
 }
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0
    button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent i = new Intent(MainActivity.this,NextActivity.class);
                     startActivity(i);            
}
            });
santosh kumar
  • 2,952
  • 1
  • 15
  • 27