logo image

How to navigate to another screen on Button click in Android

Views: 26

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);

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 {

    protected void onCreate(Bundle savedInstanceState) {

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

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