How do we switch screens in Android

This is a simple way of getting to another screen and it’s meant for beginners to get a good feel for working with intents. An intent is a way of telling our app what to do next and they can be used for many things, like going in to take a picture, make a call, send an sms, open Android settings, launching another app,... We will use it for opening a screen in our app.

Start a new project with following these steps

1. Create a new project

Create Android Project

2. Select a target devices

Target Android Devices

3. Select an empty activity to start with

Empty Activity

4. Name your activity

Configure Activity

First we will prepare our activity

1. Open the Layout Editor for MainActivity

Layout Editor


2. Just to make it simple for us to add buttons on the screen, we will change the ConstraintLayout to a RelativeLayout

• Switch to text mode

Text Mode 

• Select tag for the constraint layout and rename it

Change Constraint Layout

Choose Relative Layout
• Switch to design mode again

Now add the first button

1. Change the button style to Colored
2. Change the color to red

• Pick a Resource

Button Style

• Change to “Color”

Resource Picker
• Add a new resource

Add a color value
• Add a red color of your choice

Choose your color

4. Change the name of this button and name it buttonRed (ID=“buttonRed”)

Add another button

1. Change the button style to Colored
2. Change the color to Green (add another color as well)
3. Name it ID=“buttonGreen” .

Our MainActivity should look like this

Main Activity

Add a new activity (SecondActivity)

1. Right click “app” in the Project tool window

New Activity

2. Now we will change the color of the activity
• Select ConstraintLayout (or main layout) in the Component Tree
• Change the background color
• For that you will probably have to change the view of properties (View all attributes)

View All Attributes

• Change it to a red color
• Since you have already added red color, you should be able to pick

Second Activity

And repeat once more for (ThirdActivity)

Change its color to green

Third Activity

It's time for coding

In we will add code that will enable us to go (navigate) from one activity to another activity.

1. Add class variables as showen on the picture bellow, this should be your first code after the class declaration

Class Variables

2. Set values to our variables in the onCreate event (this event is already created for you, your code should go after )

Find buttons

3. Add onClickListeners for red button (buttonRed.set....) and add a code for a new intent inside that code, that will start a SecondActivity


4. Repeat for green button as well Add listener and a intent for ThirdActivity.

5. Test it (Once you are on a colored screen you can go back with the “Back” button)