Skip to content
Coding Fanatic
  • Android Development
  • News and Reviews
  • Road2Pro
  • Tutorials
  • Services
  • About

How to Add an Activity, 43/100 Days of Code

  • September 2, 2019September 2, 2019
  • by Richard Clarke

Adding Activities to apps gives them more versatility for performing tasks. Today, I show you step by step how to add them to your apps.

In this tutorial, I show you how to add additional Activities to your Android Applications. You can find a video of this at the bottom of the page, code snippets throughout the page, and a link to my source code here.

NOTE: Remember to click Apply Changes in the Android Studio toolbar to update your app! 

Beginners: You can download this project from the link above and open it in Android Studio while you follow along with the video.
Advanced: I’ve included steps throughout the page along with code snippets. Feel free to use the video as a reference as well.

Requirements

  • Android Studio and a newly created project with an empty Activity
  • An Android Virtual Device
  • BONUS: An IDE of your preference if you don’t want to use Android Studio to edit your code

Step 1: Add two new Activities and layouts to your project.

  • Right-click your classes and select to add each new class and each new layout.
  • For this example, we will call the two classes Second.class, and Third.class.
  • We will call the two layouts second.xml, and third.xml.

Step 2: Update the AndroidManifest with the two new Activity names. You can update your AndroidManifest.xml file using the code below

  • Add Activity Tags
  • Use the android:name XML attribute to add .Second and .Third for the new Activities.
  • NOTE: You may see red squigglies under these Activity names. You can ignore this for now.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.clickingactivities">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Second"></activity>
        <activity android:name=".Third"></activity>
    </application>

</manifest>

 

Step 3: Import the classes needed to make changes within the app in your MainActivity.java file. See the snippet after Step 5 for the entire MainActivity.java file.

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

 

Step 4: Set the title of the MainActivity as Act One. See the snippet after Step 5 for the entire MainActivity.java file.

getSupportActionBar().setTitle("Act One");

 

Step 5: Repeat steps 3 and 4 for the Second.java and Third.java files. See the code in MainActivity.java below.

  • Import the classes from Step 3
  • Set the titles as “Act Two” and “Act Three” respectively using the method in Step 4
package com.example.clickingactivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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);

        getSupportActionBar().setTitle("Act One");
    }
}

 

To see this in action, watch the YouTube video below. In the next part of this tutorial, we will configure buttons to open our new Activities.

Join the mailing list to see updates like this every week!
Coding Fanatic

Related

Why TextViews Are Clickable, 42/100 Days of Code
Clicking to Start Activities, 44/100 Days of Code
Richard Clarke
Richard is currently a developer using Java and Android Studio to build custom applications for Android. Richard tutored in mathematics at Montgomery College, and worked in Information Technology before entering the Software Development industry as a Quality Engineer.
100daysofcode Android Android Studio Java Tutorial

Related articles

Your New Favorite Stopwatch App!…
Two for Two BABY! 7…
…But the App is for…
Play Store Blues – 5…
Top 4 Tips for Android…
No Dice – 3 of…

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • GitHub
  • YouTube
  • X
  • Instagram
  • LinkedIn
My Tweets

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • X
  • GitHub
  • LinkedIn
  • YouTube

Archives

Theme by Colorlib Powered by WordPress