Clicking to Start Activities, 44/100 Days of Code
Android apps require a little configuration before they’re ready to start any added Activities.
In the second part of this tutorial, I show you how to start the Activities added in the previous section. 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: This tutorial assumes you have read/watched all previous Android Development tutorials. If not, feel free to follow along with the video at the bottom of the page. Remember to Apply Changes in Android Studio 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
BEFORE WE CONTINUE: If your AndroidManifest.xml contains red squigglies underneath the .Seocnd and .Third activity names, be sure to import and extend the AppCompatActivity class in both the Second.java and Third.java classes respectively.
Step 1: Replace the default Hello World TextView in activity_main.xml with a Button. You can update your activity_main.xml file using the code below. For more information on creating Buttons, click here.
- Change the tag from a TextView to a Button
- Change the text to read “Click here to start Act Two”
- Change the id to startActTwo
- NOTE: In this example, we will tell our app to use Act One to open Act Two, Act Two to open Act Three, and Act Three to open Act One
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/startActTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click here to start Act Two" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
Step 2: Using these changes, repeat Step 1 for the Buttons in second.xml and third.xml layout files
- Change the text in second.xml to read “Click here to start Act Three”
- Change the id in second.xml to startActThree
- Change the text in third.xml to read “Click here to start Act One”
- Change the id in third.xml to startActOne
Step 3: In the MainActivity.java file, configure a button to start a new Activity when clicked. You can update your activity_main.xml file using the code below. For more information on creating Buttons, click here.
- Declare the Button and Intent objects in MainActivity.java within the class before overriding the onCreate() method.
- Instantiate the Button object in the onCreate method
- Configure the onClickListener to start the Second.class Activity when the button is clicked.
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 { Button buttonStartSecond; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().setTitle("Act One"); buttonStartSecond = findViewById(R.id.startActTwo); buttonStartSecond.setOnClickListener(new View.OnClickListener() { @Override public void onClick (View v){ intent = new Intent(MainActivity.this, Second.class); startActivity(intent); } }); } }
Step 4: Using these changes, repeat Step 3 for the Second.java and Third.java files.
- In Second.java, change the Button object name to buttonStartThird. Be sure to replace all instances of buttonStartSecond with this new name for the button.
- TIP: Good naming conventions like these will make your code easier to read!
- In Second.java, instantiate buttonStartThird using the id R.id.startActThree
- In Second.java, update the onClick method within the setOnClickListener method with (Second.this, Third.class)
- In Third.java, change the Button object name to buttonStartFirst. Be sure to replace all instances of buttonStartSecond with this new name for the button.
- In Third.java, instantiate buttonStartFirst using the id R.id.startActOne
- In Third.java, update the onClick method within the setOnClickListener method with (Third.this, MainActivity.class)
Step 5: Test opening the Activities.
- Now that the buttons are configured, click the button in the MainActivity to start Second, click the button in Second to start Third, and so on.
Our app is now able to open Activities with the click of a button. However, the only way to return to the MainActivity is to use the back key, or the button in the Third Activity.
In order to make our Second and Third Activities children of MainActivity, we’ll need to define MainActivity as the parent. In the next part of this tutorial, we’ll add back arrows to the Second and Third Activities to return them to MainActivity.
To see this in action, watch the YouTube video below. For the source code click here.
Join the mailing list to see updates like this every week!
Coding Fanatic