Android: How To Navigate Between Activities or Screens
Hey guys! I uploaded this video to show you how to go from one activity to another activity. This is basically going from one screen to another screen. There are going to be some prerequisites, though. They are:
Java for your computer
IDE (Like Android Studio, which I will be using, Eclipse, AIDE, Kivy, etc.)
A JDK (Java Development Kit) and/or an SDK (Software Development Kit)
First of all, you add a button in the activity. The button's xml should look something like this:
Your final activity_main should be something like this:
Which should look something like this:
Now, once we have done the layout for the app, we can now start on the coding part. First of all, we are going to define the variable. That is done like this:
Next, in the onCreate/main method, we will initialize the variable toSecondActivity. We do it like this:
Once we are done with that, we add an onClick listener like this:
Now, we are going to add an Intent. Before we add one, the following image will explain what an Intent is:
We add an Intent by adding the following statement:
To go from MainActivity to SecondActivity, we will add:
By now, your MainActivity should be something like this:
Now, create a new activity called, SecondActivity. It should seem something like this:
Once you've done this, add a TextView in the activity_second layout file. It should be something like this:
Now, your final activity_second layout file should be something like this:
The activity_second layout will create a layout like this:
NOTE: You don't have to do this unless you want to do something with the text in the second screen/activity!
Now, we can start coding on the SecondActivity class. We will first define the variable which is done like this:
Then, in the onCreate method, we will initialize the variable like this:
The final SecondActivity class should be something like this:
Congratulations! We are now done! We will now test our app. It should look something like this:
留言