What are the activities in applications and how are designed in Android apps?
10 minute(s) read
Published on: Jul 31, 2021
Updated on: Dec 14, 2021
This way, we know that every page displayed in applications is an activity. To better understand this issue, it is better to use an example, consider that we have designed an application that asks the user to enter his information on the first page. After entering the user information, another page opens that the user can Have access to the main content of the application. So, to get more acquainted with the activities, follow this educational article.
As explained above, it should be noted if we consider an application that asks the user for information to log in; This application consists of two activities. And every activity comes from Back-end and Front-end.
Front-end activity is the part that is displayed in appearance, and users work with it. In other words, it can be said that it is the user interface between the application, the user, and the operation that the user wants to do. It should be noted that for the design of the user interface of each activity, the UI codes are placed by XML codes and in a file with XML extension in the project created to design the application.
Back-end activity is the same code inserted to design the application, and these codes are hidden from the user and are not displayed to them. This code is written using Java and is contained in a file with the extension java. Placed.
For the code inserted behind the scenes to integrate with the user interface designed for the application and be relevant, we need to define the XML file inside the java file.
1- We create a new project in Android Studio to check their performance.
2- After creating a new project in Android Studio, we will see that Android Studio has created an activity by default, including activity_main.xml and MainActivity.java.
The names of all these files can be changed, but we will not change their names in this part of the tutorial.
3- To see the lines that are next to the code on the left, we must click on that right part and select the Show line numbers option.
4- After activating this feature, you can see that the code inserted in the first line is for the package name that we specified when creating the project.
5- You can easily check the active structure together using the line number after activating this feature. Lines 3 and 4 are for classes imported into activity by default.
6- It should be noted that whenever imported classes are displayed as… import, we must click on the + sign on the left side so that the list of imported classes is not collected and displayed.
7- You can also do this through Android settings and go to File - Settings - Editor - General - Code Folding to prevent the list of imported classes from being compiled.
8- In addition to changing and determining whether the imported classes are active or not, you can also change other items. This can be easily done using the Show code folding outline option.
9- AppCompat Library is a library developed and updated by Google. The Android.support.v7.app.AppCompatActivity class also belongs to the same library.
How can we add libraries to the project?
1- Online method of adding a library to the project:
There are two ways to add a library to a project online, and there are two ways we can add a library to a project online.
- Manual method:
In adding the library to the project online, we must add the code line entered by the library developer in the build.gradle file (Module: app) inside the dependencies. After adding them, we must sync it so that by doing this, the library will be downloaded from the server and added to our project. It should be noted that this line of code provided by the developer starts with compile, and then the name of the library is inserted.
You can also add and sync the library in the project and the dependencies section as follows:
After selecting the sync option, Android Studio will automatically start downloading the library from the server and placing it inside the project.
Early on, MavenCentral was a library repository that Google later modified. Most library download repositories are now centers, and most library developers upload their files and libraries to this repository.
What is a library?
Libraries are collections of code collected by developers in files and packages. The advantage of using and creating libraries is that we can increase the speed of designing and developing applications if we use libraries. This increase in speed is since libraries are a collection of ready-made code, and the application developer no longer needs to write the code.
In other words, we can say that we use ready-made libraries that have ready-made codes, and by doing so, the application developer no longer needs to enter codes. He can use ready-made codes.
Application developers can use http://github.com to access different types of libraries. There are many libraries on this site that most application developers use.
Another advantage of using this library in application development is that because many application developers use these libraries, bugs and errors are reduced. Or in case of any bugs and errors, because many application developers use these libraries, they may also provide their solutions before you, and you are ready to go to both the library and the collection. Access the codes and how to fix the bugs.
- Automatic method:
In this way, from the online method of adding a project to Android, we must right-click on the app option and then select the Open Module Settings option. The shortcut key to do this is F4.
1- In the new Module window that opens, the app option must be selected. Select it if it is not selected.
2- Then go to the Dependencies section.
3- Click + and select Library Dependency.
4- On the next page that we have entered, we must select the library's name.
2- Offline method of adding a library to the project:
In this method, we must add the library file we received, and its extension is .jar to the project.
It should be noted that before doing anything, you must change the display of Android to Project Files.
There is a folder called libs next to the build and src folders. It is best to copy the library file saved with the .jar extension and paste it into this folder. Even if this folder does not exist, we can create it ourselves. Right-click on the app folder, then go to New - Directory. We create a new path called libs.
The libs folder exists between the build and src folders.
This method is like the manual method of adding the library to the project online, with the only difference that in this method, we must call the file from the libs path.
Add the code below to the dependency's method.
We should enter the library file name instead of Library_file_name.jar. Like the following:
But in the automatic method, after moving the file to the libs path, instead of putting the code in the build.gradle section, go to Open Module Settings - app to the dependencies tab.
After going to the desired tab, click on the + sign, and using the File Dependencies option, we can access all paths such as libs.
10- It is noteworthy that in examining the structure of the activity, we see that the fourth line of the Android.os.Bundle class that has been imported is responsible for transferring information between the activities and storing them.
11- In line 6, we have the main activity class inherited from the AppCompatActivity class by default. In other words, we can say that the class that was imported in the third line is used in the sixth line.
Imports are done automatically by the system, and there is no need to type them manually. For example, if you want to delete MainActivity and then create it yourself, classes will be imported automatically if it inherits from AppCompatActivity.
12- Selecting AppCompatActivity from the list of choices will add a line of code 4.
13- If you write the class manually but not imported, a message will be displayed for you with the theme that presses the Alt + Enters key combination to import and confirm.
14- Line 6 also shows the onCreate method. It should be noted that almost everything in Activities starts from this method, so we must define all our needs and whatever we want to do inside this method.
15- If we want to apply a change that accesses the main class inherited from the Override method, AppCompatActivity, we use the super command.
16- The setContentView () method is also responsible for displaying activity content to users, which we define as follows.
The R character is the word Resource that is used in Java code embedded in Android Studio to use elements.
17- We declare the layout to R and say that we want to display a layout.
18- Add the name of the XML file, which is the main page without the same activity_main.xml extension, to the MainActivity.java activity. Like the following:
This was an active training and study of its structure. After reading this tutorial, you can access the inserted code line and know what each of them has a task and what they are for.About our Android and iOS app development services