Developing mobile applications on the Android platform begins with understanding the basic architecture, where the key building block is Activity. When newbies wonder how to add a page to Android Studio, what they often have in mind is creating a new screen that the user will see when interacting with the app. This is a fundamental skill, without which it is impossible to build even the simplest interface consisting of several screens.

Modern development environments provide powerful tools that automate routine tasks, such as generating code templates and linking layouts. However, blindly using build wizards without understanding what's going on under the hood can lead to errors in the application's architecture. In this article, we will look in detail at the process of adding a new screen, from choosing a template to setting up navigation between components.

You will learn not only technical steps, but also best practices that will help you avoid common mistakes when linking the user interface with the app logic. Having the development environment ready and understanding the project structure is the key to a successful start.

Preparing the project and selecting the Activity template

Before you start creating a new screen, you need to make sure that your project is open correctly and synchronized with Gradle. In the project structure window, which is usually located on the left, find the folder Android Studio opened correctly and synchronized with Gradle. In the project structure window, which is usually located on the left, find the folder app, and inside it a directory java or kotlin, where the source code of your application is stored. This is where new classes will be created.

To add a page, the most efficient way is to use the built-in creation wizard. Right-click on the package with your code, select New, and then Activity. A gallery of templates will open in front of you, offering various options for implementing screens. The choice of a specific template depends on what functionality the new screen should perform.

  • ๐Ÿ“ฑ Empty Activity โ€” creates a blank screen without a preset interface, ideal for completely customizing the design from scratch.
  • ๐Ÿ“ Empty Views Activity โ€”analogous to an empty activity, but using a new approach to presenting the interface without the required XML layout in older versions.
  • ๐Ÿ—บ๏ธ Navigation Drawer Activity โ€” generates a screen with a side slide-out menu, which is useful for the main pages of complex applications.
  • โš™๏ธ Settings Activity โ€”a template for creating a settings screen with a ready-made structure of user preferences.

If you are creating a standard form, list or detailed view screen, most often choose Empty Activity. This gives maximum flexibility. After selecting a template, click the button Nextto go to the configuration of the parameters of the component being created.

โš ๏ธ Attention: Do not select a template Basic Activity for simple screens unless you need a floating action button (FAB) and a complex toolbar by default. Extra code can confuse a novice developer and complicate project support.

๐Ÿ“Š What programming language do you use in Android Studio?
Kotlin
Java
C++
Other

Configuring new screen parameters

At the configuration stage The creation wizard will prompt you to enter several critical parameters that will determine how the new class will be integrated into the project. The first thing you need to do is set Activity Name the name of the class in the code. It is recommended to use the CamelCase format, for example ProfileActivity or SettingsScreen, so that the name reflects the essence of the screen.

Next, the system will prompt you to specify the name of the interface layout file in the field Layout Name. By default, it is generated automatically based on the activity name, but you can change it. Make sure that the correct implementation language is selected: Kotlin or Java, depending on your project settings. An error in choosing a language will result in the code being written in an unintended syntax.

Particular attention should be paid to the field Launcher Activity. This checkbox determines whether this screen will be the entry point to the application at startup. Only one activity in the project should have this flag set, otherwise the system will not understand which screen to start working from. For additional pages, this option should be cleared.

๐Ÿ’ก

Use the "Activity" prefix in the class name (for example, LoginAcitivity) to instantly distinguish interface components from supporting logic classes or data models.

After checking all parameters, click Finish. Android Studio will automatically generate two main files: an activity class containing the logic, and an XML file describing the visual part. This process takes a matter of seconds, but saves hours of manual configuration.

The structure of the generated files and their purpose

After completing the wizard, new files will appear in the project that need to be understood for further work. An activity file (for example ProfileActivity.kt) contains code that controls the lifecycle of the screen. Here you will process button presses, load data and respond to device rotations.

Inside this file you will see a method onCreatethat is called by the system when creating the screen. It is in this method that the key action occurs - layout binding. The line setContentView(R.layout.activity_profile) tells the system which XML file needs to be rendered on the smartphone display.

Component Location Purpose
Activity class app/java/.../ProfileActivity.kt Managing the logic and life cycle of the screen
Layout app/res/layout/activity_profile.xml Description of visual elements (buttons, text)
Manifest app/manifests/AndroidManifest.xml Registration of activity in the Android system
String resources app/res/values/strings.xml Storing text titles and inscriptions

The layout file opens in the editor Layout Editorwhere you You can visually drag and drop elements or edit the XML code manually. Separating logic and presentation is one of the main principles of the Android architecture, which makes it easier to test and change the design without the risk of breaking the app code.

โš ๏ธ Attention: If you change the name of the layout file manually in Explorer, be sure to update the reference to it in the method setContentView inside the activity class, otherwise the application will crash with an error on startup.

Interface design in Layout Editor

The visual part of the page is created in a file with the extension .xml, located in the folder res/layout. Android Studio offers two operating modes: Design (visual editor) and Code (XML text editor). For beginners, it is recommended to start with the visual mode, where interface elements can be added by dragging and dropping from the component palette.

The basis of any layout is ConstraintLayout a flexible layout system that allows you to position elements relative to each other and screen borders. When you add a button or text field, you must set constraints, otherwise when run on a real device, the elements may collapse to the upper left corner.

  • ๐ŸŽจ Use TextView to display headings and static text on the page.
  • ๐Ÿ–ฑ๏ธ Add Button or ImageButton to create interactive controls.
  • ๐Ÿ“ฅ Apply EditText, if the user needs to enter data, for example, login or comment.

It is advisable to assign a unique one to each interface element id, for example, @+id/btn_submit. This identifier is necessary in order to find an element in the activity code and attach an event handler to it. Without an ID, programmatic control of the widget is impossible.

Why shouldn't you use absolute coordinates?

Using fixed coordinates (in pixels) leads to the interface breaking down on screens with different resolutions or pixel densities. Always use relative positioning and responsive layouts.

Registering an Activity in AndroidManifest.xml

Creating the class and layout files is only half the battle. In order for the Android operating system to know about the existence of a new page and allow navigation to it, the activity must be registered in the manifest file. Open the file AndroidManifest.xml in the folder manifests.

When using the creation wizard, an entry is added automatically, but it is useful to know what it looks like. Inside the tag <application> there must be a tag <activity> with an attribute android:namethat indicates the class. If you plan to launch this activity from another part of the application, registration is required.

<activity android:name=".ProfileActivity"

android:exported="false" />

The android:exported attribute determines whether other applications can launch this activity. For internal screens, the value should be false. If this is the main page or a page that responds to external links, it is installed true and intent filters are added.

โš ๏ธ Attention: Forgotten registration of activity in the manifest will lead to the application crashing with an error ActivityNotFoundException at the moment of trying to go to this screen. Always check the manifest after manually creating classes.

โ˜‘๏ธ Checking activity registration

Done: 0 / 4

Organizing navigation between pages

In order for the user to get to the created page, it is necessary to implement a transition (navigation) from the current activity. In Android this is done using an object Intent. Intent is a message to the system about the intention to perform an action, in this case - to open another screen.

The code for the transition is usually placed inside a button click handler. First, an Intent instance is created, which specifies the context of the current screen and the class of the target activity. Then the method is called startActivity().

val button = findViewById<Button>(R.id.btn_go_profile)

button.setOnClickListener {

val intent = Intent(this, ProfileActivity::class.java)

startActivity(intent)

}

If you need to pass data to a new page (for example, user ID or text), use the method putExtra on the Intent object. On the receiving side, this data is retrieved through an object intent in a method onCreate. This is a standard mechanism for exchanging information between screens.

Frequent errors and debugging methods

In the development process, beginners often encounter common problems that are easy to solve if they know the reason for their occurrence. One of the most common mistakes is NullPointerException when trying to find a widget by ID. This happens if the ID in the code does not match the ID in the XML file or if the method findViewById is called before setContentView.

Another problem is a memory leak or the interface not displaying correctly when the screen is rotated. By default, Android recreates the activity when the device's orientation changes, which may result in the loss of entered data. To save state, use the mechanism onSaveInstanceState or the ViewModel architecture.

Always check the logs in the window Logcat when the application crashes. The exact line number and exception type will be indicated there. Don't ignore yellow Warnings in the code editorโ€”they often indicate potential performance or compatibility issues.

Why is my new screen black or blank?

Most often this means that an incorrect layout resource is specified in the method setContentView or there are no visible elements with the specified sizes in the XML file itself. Check that the root element of the layout has layout_width and layout_height set to match_parent.

Is it possible to add a page without using XML?

Yes, in modern versions of Android you can create an interface programmatically using pure code (Code UI) or using declarative framework Jetpack Compose. In this case, the XML file is not created, and widgets are added directly in the activity code or composetables.

How to transfer the result of the work back to the previous screen?

For this, the method startActivityForResult (obsolete) or the new API Activity Result Launcheris used. The target activity calls setResult before closing, and the source activity receives data in a special callback.

Do I need to remove the activity from memory manually?

No, the Android system itself manages the life cycle and memory. However, you can call a method finish() inside the activity to close it and remove it from the task stack if the transition to it was temporary (for example, the login screen).