Android Tutorial – Switch Between Activity With Android Button Click For Beginner

Seegatesite – continued on introduction android programming tutorial that I have discussed in previous articles, this time I will share android programming tutorial on how the button to switch between activity to another activity. In developing Android App we may be facing a situation where we need to move between the Activity (Screen) to another activity. Before further discussion, I will explain a little bit about the activity on android.

What is Activity on Android ?

Activity represents one screen with a user interface. For example, A Messaging application has activity inbox list, there is other activity such as create new message.

Each activity always begins with a callback method onCreate (). Sequence callback method from start activity until the end of the activity can be seen in the following diagram lifecycle activity below

Activity Lifecycle Android Tutorial Switch Between Activity With Button Click
Activity Lifecycle (source :

Callback method defines an event, we don’t need to implement all the callback method. However, it is important and necessary to understand each function of the Callback method, so that our application can run your expectation.

onCreate() : This method was first called when the first activity begins.
onStart() : This method is invoked when the activity is already visible to the user.
onResume() : This method is invoked when the activity began to interact with the user.
onPause() : This method Called when the activity stops while not accept user input and does not execute any code.
onStop() : This method is invoked when the activity is not visible to the user.
onDestroy() : This method is called before an activity is disabled.
onRestart() : This method is invoked after the activity stopped and displayed again by the user.

Android Tutorial How to Switch Between Activity With Android Button

1. Create new project with name : SwitchActivity like following picture below

Create New Project Android Android Tutorial Switch Between Activity With Button

If you guys don’t know how to create a new project on android studio. please read my previous article Introduction Android Studio for Beginners.

2. Add new textView and buttons on the layout activity_main.xml or copy the following script on your activity_main.xml / content_main.xml

3. Create a new layout as second_activity.xml and copy the following script

4. Then open and copy the following script

[php highlight=”8,11,12,16,21,22,24,25,26,27,28,29,30″]
package com.example.sigit.switchactivity; // change with your package name
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.content.Intent;

public class MainActivity extends AppCompatActivity implements OnClickListener {

Button mybutton;
protected void onCreate(Bundle savedInstanceState) {
mybutton = (Button) findViewById(;
public void onClick(View v){
switch (v.getId()){
Intent i=new Intent(MainActivity.this, SecondActivity.class);
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
return true;
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == {
return true;
return super.onOptionsItemSelected(item);

Description :

– On line 16, add a new variable “mybutton” as a button
– On lines 20 and 21, to declare a variable mybutton with the button on activity_main.xml
– Script to open another activity found on line 27 and 28

Intent i=new Intent(MainActivity.this, SecondActivity.class);

5. Create a new class named and copy the following script

Description :

finish() function will close and return to the activity_main.xml

6. Add a new activity in android manifest as the following script

Description :

Each new activity to be accessed must be registered with the android manifest

Run App and get result like belowSwitch Between Activity Tutorial Android For Begginer

Thus article about Android Tutorial – Switch Between Activity With Android Button Click For Beginner, hope useful 🙂

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.