How to Add an Activity, 43/100 Days of Code
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