Android studio button onclick new activity

A common task!

To create a button in Android Studio that opens a new activity when clicked, follow these steps:

Step 1: Create a new activity

Step 2: Add a button to your main activity

Step 3: Define the button's onClick listener

Here's an example:

// In your main activity's Java code
public class MainActivity extends AppCompatActivity {
    private Button button;

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

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Start the new activity
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

Step 4: Declare the new activity in your AndroidManifest.xml file

That's it! You should now have a button that opens a new activity when clicked.