Day 3: Navigation Among Activities in Android Project

Introduction

 
Fundamentally, whenever you learn any App development, it is the most important part to learn how to navigate from one page to another. Here, in Android, every page is considered as an Activity. Thus, we have our title as Navigation among Activities.
 
The agenda will be:
  • First, we will add a Java file and name it Second.java.
  • Then, we will add an XML file that will define the UI of that second activity and the name would be second.xml
  • After that, we will link the Java and XML files to each other to act as one activity using setContentView()
  • Add a Button in Main Activity.
  • On clicking that button, the app will navigate to the second activity.
  • Finally, we specify the second activity in the manifest file. Since every activity must be there in the Manifest file.
Procedure
 
Step 1
 
Right-click on the package name under the /src folder. Then select New > Class.
 
Class
 
And then add the class name as SecondActivity and choose Superclass as Activity. Either you write android.app.Activity or choose it from Browse.
 
Browse
 
And, click on Finish.
 
Finish
 
Finally, we have these Java files under the package.
 
Step 2
 
We will now add an XML Layout file.
 
Either follow this procedure.
 
procedure
 
Under the res folder, right-click on the Layout folder then select the New > Android XML file.
 
Or, just click on the button.
 
button
 
And, name the XML layout.
 
XML layout
 
I have named it second_activity with Linear Layout as default.
 
Convention: Whenever you are naming any XML file then you must use lower-case letters, numbers, and underscore (_) only.
 
Last, we will click on Finish.
 
finish Botton
 
Step 3
 
Now, it's time to link the XML layout file to the Java file.
 
So, open the SecondActivity.java under the src/ folder.
 
src folder
 
Next, we will add an onCreate() method first.
 
onCreate Method
 
Tips: Whenever you are creating any pre-defined method then always use Template Proposal (or, Intellisence), in other words, Ctrl+Spacebar.
 
For example: Type void OnCreat <PRESS> Ctrl+Space and <ENTER>.
 
Now, why are we creating the onCreate() method?
 
This is the first question everyone has in their mind. So, onCreate() is the first method called whenever an activity is called. So, whenever the second activity is called, then its onCreate() method will be executed first.
 
Analogous: It's the same as Form_Load() in C# (or the .NET Language).
 
Inside onCreate() we are setting the XML layout file. We specify that this Java file is linked to the XML layout file, in other words second_activity.xml . And this has ben done by the setContentView() method.
 
And, why the heck did we have to write R.layout.second_activity?
 
Actually, it is a path of second_activity. And, it is similar to res/layout/second_activity.
 
Note: Whenever we want to access the res folder we use the R keyword to point to it.
 
Step 4
 
We will now add a button in the main_activity file.
 
main_activity file
 
Just drag and drop a button to the main_activity file. Then click on the Toggle Fill width to make it screen sized.
 
Toggle Fill width
 
And, now we will move to the Property windows to change the Button's text.
 
Property windows
 
And, it looks like something this:
 
looks
 
For now, we have completed the UI section and we will now move into Java code.
 
Whenever you add any views in the XML layout, then you must add its instance in the respective Java file.
 
So, for this, we have added one button. And now we add an instance for this.
 
add instance
 
All the time you need to write this code for all the views in XML layout. For that, we have a simple process to do it quickly without any error.
 
First, type Button <Button Instance Name> = find CTRL+SPACE <ENTER>.
 
Then, you have Button button1 = findViewById().
 
Get inside braces, type R CTRL+SPACE id CTRL+SPACE button1 (whatever).
 
By doing this you have two underlined errors.
 
The first is on Button and the second is on finViewById(). For fixing get your cursor there and press CTRL+1 to fix the error.
 
Since you need to error then, do it for twice for respective places.
 
Tip: Whenever you are having any curly error under some line always try to fix it by Quick Fix (Ctrl+1).
 
Step 5
 
Open MainActivity.java.
 
Here, we will establish the Button click event, in other words, Click Listener in teams of Java if I need to say.
 
So, we will add this in the onCreate() method.
 
add Method
 
Using CTRL+SPACE we added this setOnCLick listener.
 
In short, it is the Click event for myButton(or, button1).
 
And, onClick() is the event handler, in other words, what will we be done when button1 is clicked.
 
So, we will add the Navigation method that will navigate to the second activity page.
 
activity page
 
If you have any kind of error, then try to fix it using Quick Fix Ctrl+1.
 
Step 6
 
Add the activity name in the Manifest file.
 
So, open AndroidMainfest.xml and add this:
 
add XML Code
 
Here, we use a period (.) to denote the package folder in /src.
 
And now Save All and Run.
 
Run
 
Output
 
If you have an Emulator, then it will take some time to start.
 
After successful completion, you have this: 
 
Output
 
And, when you click on the button then you are moved to Next Activity.
 
Activity
 

Conclusion

 
In this, we saw how Navigation among Activities works. And, for any confusion, you can check with the source code file.
 
Previous article: Day 2: Structure of Android Application Project