In your onCreate method, you should do something like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.your_button_id);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
}
}
The your_button_id
is the ID of the button in your MainActivity
and the code above tells the button to startActivity
when the button is pressed. In this case, the new activity is MainActivity2
.
For more information, you should go through the tutorials available: http://developer.android.com/training/basics/firstapp/index.html
solved Click on button in MainActivity and go to screen on MainActivity2 [closed]