How to navigate to another screen on Button click in Android


First, create two activities and add a button to first activity as shown in the following image. In this case, my button's id is button

android button with id

Now open the code for the first screen and access the button using id. Add an OnClickListener to the button and write following code for opening the second screen in OnClickListener.

Intent i = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(i);

The complete code for the First screen looks something like below

package test.com.testapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View view) {
                
                Intent i = new Intent(getApplicationContext(),Main2Activity.class);
                startActivity(i);
                
            }
        });
    }
}

Output:

info-about-image